warfighter67

Short Java RndNum

Oct 6th, 2012
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.22 KB | None | 0 0
  1. /* @author
  2.  * October 5, 2012
  3.  * High-Low Game part 2
  4.  * Description: Part 1 of the High-low Game. Generates a random integer
  5.  * from 0 to 500 that the user guesses. Outputs "higher" or "lower" until
  6.  * number is guessed correctly.*/
  7. import java.util.*;
  8. public class Expressions{
  9.     public static void main(String[] args){
  10.         Scanner scan_in = new Scanner(System.in);
  11.         int rndNum = 0, guess = 0, numGuesses = 0;
  12.         int ruledOutNumLow = 0, ruledOutNumHigh = 500;
  13.         int rounds = 0, totalGuesses = 0;
  14.         double averageGuessAmount = 0.0;
  15.         String answer = ""; char response;
  16.         boolean validAnswer = false;
  17.         do{
  18.             numGuesses = 0;
  19.             ruledOutNumLow = 0; ruledOutNumHigh = 500;
  20.             rndNum = (int) (Math.random() * 500);
  21.             do{
  22.                 System.out.print("Guess a number between 0 and 500, -1 to give up: ");
  23.                 guess = scan_in.nextInt();
  24.                 if(guess == -1) break;
  25.                 if(guess != rndNum){
  26.                     if(guess < rndNum){
  27.                         if(guess < ruledOutNumLow){
  28.                             printThis(ruledOutNumLow, ruledOutNumHigh);}
  29.                         else{
  30.                             ruledOutNumLow = guess + 1;
  31.                             System.out.println("Your guess is too low.");}}
  32.                     else{
  33.                         if(guess > ruledOutNumHigh){
  34.                             printThis(ruledOutNumLow, ruledOutNumHigh);}
  35.                         else{
  36.                             ruledOutNumHigh = guess - 1;
  37.                             System.out.println("Your guess is too high.");}}}
  38.                 numGuesses++;}while(guess != rndNum);
  39.             if(guess != -1){
  40.                 System.out.println("Congrats! You guessed the number in " + numGuesses + " tries!");
  41.                 rounds++;
  42.                 totalGuesses += numGuesses;}
  43.             do{
  44.                 System.out.print("Play again? (Y/N): ");
  45.                 answer = scan_in.next();
  46.                 response = answer.charAt(0);
  47.                 if(response == 'Y' || response == 'y' || response == 'N' || response == 'n')validAnswer = true;
  48.                 if(!validAnswer)System.out.println("Please enter Y or N.");}while(!validAnswer);}while(response == 'Y' || response == 'y');
  49.         averageGuessAmount = (double)totalGuesses/rounds;
  50.         System.out.println("Thanks for playing!");
  51.         System.out.println("You took an average of " + averageGuessAmount + " guesses per round.");}
  52.     public static void printThis(int low, int high){System.out.println("That number has already been ruled out by prior guesses. Only the numbers " + low + " through " + high + " are valid guesses.");}}
Advertisement
Add Comment
Please, Sign In to add comment