While Loops #67 (It's an If statement within an If statement.)

Code

/// Name: Amir Salehi
/// Period: 7
/// Program Name:Reverse Shot Hi Lo
/// File Name:ReverseShotHiLo.java
/// Date Finished: 12/14/2015

 
import java.util.Random;
import java.util.Scanner;

public class ReverseShotHiLo
{
	public static void main ( String[] args )
	{
		Random r = new Random();
        
        Scanner input = new Scanner(System.in);
        
        
        int hi = 1000;
        int low = 1;
        int guess = (low + hi) /2 ;
        String n;
        System.out.println("");

        System.out.print("Think of a number from 1 to 1000.  I'll try to guess it.");
         System.out.println("");
        System.out.println("My guess is " + guess + "." + "Am I too (h)igh, too (l)ow, or (c)orrect? ");
        n = input.next();
        
        System.out.println("");
while (! n.equals("c")) {
if ( n.equals("h"))
    hi = guess;
    
    if (n.equals("l"))
        low = guess;
    guess = (low + hi) /2 ;
             System.out.println("");
        System.out.println("My guess is " + guess + "." + "Am I too (h)igh, too (l)ow, or (c)orrect? ");
        n = input.next();
    

    
        System.out.println("");

}
    

    
    if ( n.equals("c")) {
             System.out.println(" Ha!  I am the greatest guesser in the WORLD!");

    }

}
    
}

    

Picture(s) of the output

Assignment 7

Back to Homepage