Assignemnt #65

Code


/// Name: Daniel Nguyen
 /// Period: 6
 /// Program Name: GuessingCounter Program
 /// File Name: GuessingCounter.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 GuessingCounter
{
	public static void main( String[] args )
	{
		Scanner keyboard = new Scanner(System.in);
        Random r = new Random();
int rng = 1 + r.nextInt(10);
        int tries;
        tries = 0;
		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();
            tries++;
		}

		System.out.println("\nCORRECT!!");
        System.out.println("It only took you " + tries + " tries.");
	}
}

    


    

Picture of the output

Assignment 3
BACK HOME NEXT