LOOP PROGRAM

LOOP PROGRAM

Hi all,
Someone please help this code.

I need a Java program that uses looping to toss a coin over and over until it comes up head 10 times. The program should also record the number of tails recorded.

Thanks.
View Answers

April 17, 2010 at 3:09 PM

Hi Friend,

Try the following code:

class Toss{
public final int HEADS = 0;
static int countH=0;
static int countT=0;

private static int face;
public static void flip(){
face = (int) (Math.random() * 2);
}
public String toString(){
String faceName;
if (face == HEADS){
faceName = "Heads";
countH++;
}
else{
faceName = "Tails";
countT++;
}
return faceName;
}
public static void main (String[] args) {
do{
flip();
System.out.println(new Toss().toString());
}
while (countH!=10);
System.out.println("Number of Tails: "+countT);
}
}

Thanks









Related Tutorials/Questions & Answers:
loop program
loop program  please tell me the code for the output 1 2 3foo 4 5bar 6foo 7baz 8 9foo 10 11 12foo 13 14baz 15foo bar 16 17 18foo . .......till 50
LOOP PROGRAM - Java Beginners
LOOP PROGRAM  Hi all, Someone please help this code. I need a Java program that uses looping to toss a coin over and over until it comes up head 10 times. The program should also record the number of tails recorded
Advertisements
Write a program to print using for loop
Write a program to print using for loop  Write a program to print using for loop * * * *  
write a program to use for loop print **********========== in c language
write a program to use for loop print **********========== in c language  write a program to use for loop print **========== in c language
Why is my program running an infinite loop? Parallel Arrays Program
Why is my program running an infinite loop? Parallel Arrays Program  Calculate and print the average (this works) Search for Joel in the list, print... an infinite loop. Why? import java.util.*; import java.text.DecimalFormat
for loop
the program control enters the loop it first does the initialization part... and then control comes out of the "for" loop and the above program ends...for loop  what is for loop   Loops are used for iteration
loop
loop  what is the difference b/w do while,while and for loop..with example
loop
loop  i want to write my name(inder) through loop in java using star
For Loop
For Loop  Write a program using a for loop that computes the following sum: 1/2 + 2/2 + 3/2 + 4/2 + 5/2 + ... N/2 N will be an integer limit the user enters. This is an example of my upcoming quiz. I need help on knowing
For loop
For loop  Hi! Please explain the following loop: for(i=0;i<a[j];i++) THANX!!   Please reply as soon as possible
for loop
for loop  how to performs the for loop when 2 inputs given like displays the triangle shape and how to take initial values or declaration values plz explain in detailed ex. 1 12 123 1234
Loop
to understand a loop my quesiton are: what du i do wrong in my code since.... (made in blueJ) Class Loop public void loopUpGeneral(int start, int stop... dk  Hi, Please check the for loop once again: for (int i
loop
loop  strong textHello can someone help me with this problem,I need to output the following code using a loop the code is as follows with the user inputs in inverted commas,thanks. ENTER CODE(XX to Stop) CODE:"D24" QUANTITY:"2
for loop
for loop  using only two for loop how can print below numbers 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 8 8 8 8 8 8 8 8 7 7 7 7 7 7 7 6 6 6 6 6 6 5 5 5 5
for loop
for loop  using only two for loop how can print below numbers 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 8 8 8 8 8 8 8 8 7 7 7 7 7 7 7 6 6 6 6 6 6 5 5 5 5
for loop
for loop  hai, I need to write java program to print numbers in "X" format
Java Break out of for loop
Java Break out of for loop       In loop programming the program flow is continued till... it out of the loop. While 'continue' label just stops the program flow at line where
Do While Loop in Java
. On the contrary in Do…..While loop, the program first execute...Do while loop in Java There are three loops most commonly used in Java e.g. While loop, Do….While loop, and For loop. These loops are used
Two Dimensional Array Program Using Nested For Loop
program. We are going to using for loop for performing the five rows and five... Two Dimensional Array Program Using Nested For Loop        This is very simple program
loop statements
loop statements  write a program that reads from input an interger n and prints the factorial of all numbers between 1 and n
loop, control structures
loop, control structures  a java program to print
loop, control statements
loop, control statements  a java program to print
For..in loop
For..in loop         In this tutorial we will study for..in loop which is similar to for each loop of C#, Java and other popular languages, in this example
Java for loop
Java for loop  Can a for statement loop indefinitely
ModuleNotFoundError: No module named 'for_loop'
ModuleNotFoundError: No module named 'for_loop'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'for_loop' How to remove the ModuleNotFoundError: No module named 'for_loop
ModuleNotFoundError: No module named 'for_loop'
ModuleNotFoundError: No module named 'for_loop'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'for_loop' How to remove the ModuleNotFoundError: No module named 'for_loop
ModuleNotFoundError: No module named 'loop'
ModuleNotFoundError: No module named 'loop'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'loop' How to remove the ModuleNotFoundError: No module named 'loop' error
ModuleNotFoundError: No module named 'for_loop'
ModuleNotFoundError: No module named 'for_loop'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'for_loop' How to remove the ModuleNotFoundError: No module named 'for_loop
For Loop - Struts
For Loop  Hi, How to use for loop in Struts2. I don't want use.... How to use the For loop in JSP. Thanks, Rajesh.  Hi Rajesh, i am sending simple use of for loop. In this example, you will see the use
for( ) loop in php
for( ) loop in php  Generally, when we use for loop in PHP or any other programming language why we use i++ and not ++i. What is the reason behind
for( ) loop in php
for( ) loop in php  Generally, when we use for loop in PHP or any other programming language why we use i++ and not ++i. What is the reason behind
Limits of "For loop"
Limits of "For loop"  Hi, How can I check the upper limit and lower limit of 'FOR' loop during unit testing using Junit. Thanks & Regards Ankit
How to write a loop and a while loop
How to write a loop and a while loop  How do I write a 1 loop and a 1 while loop for the example code: public boolean isTheFirstOneBigger (int num1, int num2) { if (num1 > num2) { return true
PHP For Loop Function
For loop Function in PHP: In for loop construct there are three parts of For loop, first Initialization part: initialize a variable (we can initialize more... or decrement the variable(s). For loop function Example:ADS_TO_REPLACE_1
LOOP-HOLE SERIES GENERATION
LOOP-HOLE SERIES GENERATION  QUESTION:- WRITE A PROGRAM TO GENERATE THE FOLLOWING SERIES CALLED LOOPHOLE SERIES:- 3,5,53,13,19,7,19,13,53,5,3
LOOP-HOLE SERIES GENERATION
LOOP-HOLE SERIES GENERATION  QUESTION:- WRITE A PROGRAM TO GENERATE THE FOLLOWING SERIES CALLED LOOPHOLE SERIES:- 3,5,53,13,19,7,19,13,53,5,3
for loop in java
for loop in java  54321 4321 321 21 1 using for loop or nested 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 5 4 3 2 1 4 3 2 1 3 2 1 2 1 1 using for loop or nested... 8 9 6 7 8 9 10 all for loop code plz friends guide me
for loop in java
for loop in java  54321 4321 321 21 1 using for loop or nested 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 5 4 3 2 1 4 3 2 1 3 2 1 2 1 1 using for loop or nested... 8 9 6 7 8 9 10 all for loop code plz friends guide me
for loop in java
for loop in java  54321 4321 321 21 1 using for loop or nested 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 5 4 3 2 1 4 3 2 1 3 2 1 2 1 1 using for loop or nested... 8 9 6 7 8 9 10 all for loop code plz friends guide me
for loop in java
for loop in java  54321 4321 321 21 1 using for loop or nested 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 5 4 3 2 1 4 3 2 1 3 2 1 2 1 1 using for loop or nested... 8 9 6 7 8 9 10 all for loop code plz friends guide me
for loop in java
for loop in java  54321 4321 321 21 1 using for loop or nested 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 5 4 3 2 1 4 3 2 1 3 2 1 2 1 1 using for loop or nested... 8 9 6 7 8 9 10 all for loop code plz friends guide me
loop example
loop example   1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 can you give me the code to get the number pyramid
For Loop/PHP
For Loop/PHP  Write a class called math. It is to have one property... 12345. How would you write this as a loop? Would I need to use the for loop or do/while loop?   PHP Factorial Example: <?php $n = 5; $i = 1; $f
adding loop
adding loop  Hi I have a program that is not compiling when I add a loop can you help me?The program below is compiling without the loop. >...:"); String p=in.nextLine(); } } And the program containing
foreach loop in php
foreach loop in php  Foreach loop in php
For loop java
For loop java  1 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 3 4 5 4 5 6 7 5 6 7 8 9 6 7 8 9 10
for loop in java
for loop in java  1 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 3 4 5 4 5 6 7 5 6 7 8 9 6 7 8 9 10 using for loop in java   Java Number Pattern 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 public class PatternExample{ public static void main
for loop
php array loop count
php array loop count  Count the array using loop in php
php array loop while
php array loop while  Using the php array while loop

Ads