Assignemnt #61
Code
/// Name: Daniel Nguyen
/// Period: 6
/// Program Name: KeepGuessing Program
/// File Name: KeepGuessing.java
/// Date Finished: 9/9/2015
// a while loop is similar to an if statement because it tests if a statement is true and runs code after that depending on it
// A while loop is different from an if statement because it continues to run the code until the statement is no longer true
// There is no int because "entry" as a variable is already defined as an int in the previous code
// When line 29 is removed it causes the while loop to never be satisfied and keeps running until it is which is never
import java.util.Scanner;
import java.util.Random;
public class KeepGuessing
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
Random r = new Random();
int rng = 1 + r.nextInt(10);
System.out.println("Try to guess the number I'm thinking of from 1-10.");
System.out.print("Your guess > ");
int entry = keyboard.nextInt();
while ( entry != rng )
{
System.out.println("\nWRONG! TRY AGAIN!!!");
System.out.print("Your guess > ");
entry = keyboard.nextInt();
}
System.out.println("\nCORRECT!!");
}
}
Picture of the output