Assignemnt #66

Code


/// Name: Daniel Nguyen
 /// Period: 6
 /// Program Name: hilolim Program
 /// File Name: hilolim.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 hilolim
{
	public static void main( String[] args )
	{
		Scanner keyboard = new Scanner(System.in);
        Random r = new Random();
        int rng = 1 + r.nextInt(100);
        int tries;
        tries = 0;
		System.out.println("Try to guess the number I'm thinking of from 1-100.");
        System.out.println("You have 7 tries");
		System.out.print("Your guess > ");
		int entry = keyboard.nextInt();

		while ( entry != rng )
		{
            if ( entry < rng ) {
			System.out.println("\nWRONG! You guess too low... Try again!");
            }
            if ( entry > rng ) {
             System.out.println("\n You guess too high try again!");   
            }
			System.out.print("Your guess > ");
            entry = keyboard.nextInt();
            tries++;
            if ( entry == rng ) {
                System.out.println("\nCORRECT!!");
                System.out.println("It only took you " + tries + " tries.");
            }
            if ( tries == 7 ) {
              System.out.println("Sorry you ran out of tries :/");  
              entry = rng;
            }
		}

	}
}

    


    

Picture of the output

Assignment 3
BACK HOME NEXT