ricky
LOOP PROGRAM
1 Answer(s)      3 years and a month ago
Posted in : Java Beginners

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 Pages:
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
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  hai, I need to write java program to print numbers in "X" format
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
Write a program to print using for loop
Write a program to print using for loop  Write a program to print using for loop * * * *  
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
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
programes on for loop
programes on for loop  a. write a program to find squares and cubes of 1st 10 integers b. write a program to to calculate factorial of a no, c. write a program to print Fibonacci series d. write a program to check whether entered
programes on for loop
programes on for loop  a. write a program to find squares and cubes of 1st 10 integers b. write a program to to calculate factorial of a no, c. write a program to print Fibonacci series d. write a program to check whether entered
programes on for loop
programes on for loop  a. write a program to find squares and cubes of 1st 10 integers b. write a program to to calculate factorial of a no, c. write a program to print Fibonacci series d. write a program to check whether entered
programes on for loop
programes on for loop  a. write a program to find squares and cubes of 1st 10 integers b. write a program to to calculate factorial of a no, c. write a program to print Fibonacci series d. write a program to check whether entered
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
loop, control structures
loop, control structures  a java program to print
loop, control statements
loop, control statements  a java program to print
using for loop
using for loop  what will be the source code of the program that the output will be printing all numbers that is divisible by 3 and 5 sample output: 3 is divisible by 3 5 is divisible by 5 6 is divisible by 3 9 is divisible by 3
nested for-loop
nested for-loop  hi.write a program that will prompt the user for a positive integer(num) and then display two triangles with num number of lines... the correct value. e.g if the user enters 2,the program must display the following
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
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
using variable loop
using variable loop  program to print alphabets from a-z along with ASCII codes of each alphabets in two columns using a character variable loop...can anyone help me
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
While loop Statement
While loop Statement  How to print a table using while loop?   The given example displays the multiplication table of the given number. This program accepts the integer value generate the table of that number. public
For Loop in Java
the multiple for loop in a program or a for loop also. When we write a loop under... the for loop for developing a application or a program. In this program we will see...;} } Output of the program :  1 22 333 4444 55555 Download For Loop Example
While Loop
. Loop makes your program readable, flexible and reliable. While loop: While loop...; statements;    } In this program you will see how to use the while loop... While Loop       Loop
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
While loop - Java Beginners
While loop  Given a number, write a program using while loop to reverse the digits of the number. Eg. the no. is 12345, should be written as 54321.  Hi friend, Code to solve the problem : import java.io.*; class
loop problem - Java Beginners
loop problem  I trying to write a program which prints out even numbers, I've got that part, however, I need to only have 5 numbers per line. ex: 2 4 6 8 10 12 14 16 18 20 I tried using a counter, but I'm missing
For loop
For loop  Hi! Please explain the following loop: for(i=0;i<a[j];i++) THANX!!   Please reply as soon as possible
stop an infinite for loop - Java Beginners
want this infinite for loop program to be stopped by another program named kill. when i run that another program kill the execution of infinite loop program
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
programes on while loop
programes on while loop   write a program to calculate sum of an entered digit write a program to find gcd and lcm of 2 positive integers   Java sum of digits Java Find LCM and GCD
for loop - Java Beginners
for loop  write a program to print the product of all the numbers from 1 to N(this is also known as factorial of the numbers
for loop - Java Beginners
for loop  write a program to print the product of all the numbers from 1 to N(this is also known as factorial of the numbers
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
for loop - Java Beginners
for loop  write a program to print the following series :10000,5000,2500,1250,625.  Hi Friend, Try the following code: class Series{ public static void main(String[] args){ int num=10000; System.out.print
Java break for loop
. Breaking or terminating the loop means bringing out the flowing program... Java break for loop       Example below demonstrates the termination of for loop under label
for loop - Java Beginners
for loop  write a program to enter two numbers and print their HCF.   Hi Friend, Try the following code: import java.util.*; class HCF{ public static int determineHCF(int a, int b) { if (b==0
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
Character count by while loop
Character count by while loop  Write the program to count the number of characters entered until a full stop (.)is keyed-in. Anyone there pls help with this question...   import java.io.*; import java.util.*; class
Writing a loop statement using Netbean
Writing a loop statement using Netbean  Write a loop program using NetBeans.   Java Loops class Loops{ public static void main(String[] args){ int sum=0; for(int i=1;i<=10;i
Square pattern in java using for loop
Square pattern in java using for loop  please explain the logic to program the following using for loop in Java: 1 3 5 7 9 3 5 7 9 1 5 7 8 1 3 7 9 1 3 5 9 1 3 5 7
Square pattern in java using for loop
Square pattern in java using for loop  please explain the logic to program the following using for loop in Java: 1 3 5 7 9 3 5 7 9 1 5 7 8 1 3 7 9 1 3 5 9 1 3 5 7
Square pattern in java using for loop
Square pattern in java using for loop  please explain the logic to program the following using for loop in Java: 1 3 5 7 9 3 5 7 9 1 5 7 8 1 3 7 9 1 3 5 9 1 3 5 7
Square pattern in java using for loop
Square pattern in java using for loop  please explain the logic to program the following using for loop in Java: 1 3 5 7 9 3 5 7 9 1 5 7 8 1 3 7 9 1 3 5 9 1 3 5 7
Square pattern in java using for loop
Square pattern in java using for loop  please explain the logic to program the following using for loop in Java: 1 3 5 7 9 3 5 7 9 1 5 7 8 1 3 7 9 1 3 5 9 1 3 5 7

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.