-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlipAgain.java
36 lines (26 loc) · 912 Bytes
/
FlipAgain.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.Random;
import java.util.Scanner;
public class FlipAgain {
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
Random rand = new Random();
String again;
do{
int flip = rand.nextInt(2);
String coin;
if ( flip == 1 )
coin = "HEADS";
else
coin = "TAILS";
System.out.println( "You flip a coin and it is... " + coin );
System.out.print( "Would you like to flip again (y/n)? " );
again = keyboard.next();
}
while ( again.equals("y") );
}
}
/*The program works for the do-while loop because,
the do-while loop executes all statements in its
block before testing the condition in the while loop.
This shows how efficient do-while loop is as compared to while loop.*/