Assignment #70 and FlipAgain
Code
/// Name: Jason Kim
/// Period: 7
/// Program Name: FlipAgain
/// File Name: FlipAgain.java
/// Date Finished:11/12/15
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;
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"));
}
}
//It works because while is at the end of the loop and "again" is stored as a value in the loop. Do-While loop checks the condition after it does the action.
Picture of the output