Assignment #70 and Flip Again?
Code
/// Name: Ali Kurland
/// Period: 6
/// Program Name: Flip Again?
/// File Name: FlipAgain.java
/// Date Finished: 10/16/2015
/// 3. The program still works when the variable "again" is not initially given the value "y". This is because a do-while loop does not check the value of "again" until it has run the code following the "do," and by this point, "again" has been assigned a value by the user.
import java.util.Random;
import java.util.Scanner;
public class FlipAgain
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
Random rng = new Random();
String again;
again = "y";
do
{
int flip = rng.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") );
}
}
Picture of the output