Tuesday, October 12, 2010

11. Explanation on GuessingRandomNumber class

Okay so I've just posted the class code here in case you didn't want to download it over at the source code page.  Copy and paste it then look at the comments, they will explain what is going on!!!
  1. import java.util.Scanner;
  2. import java.util.Random;
  3. public class GuessRandomNumber
  4. {
  5.     public static void main(String[] args)
  6.     {
  7.         //Create a new scanner object called input
  8.         Scanner input = new Scanner(System.in);
  9.         //Create a new random object called generator
  10.         Random generator = new Random();
  11.         //Here we will count how many guesses the user has
  12.         int guessCounter = 0;
  13.         //This boolean is true if the user hasn't won
  14.         boolean userHasNotWon = true;
  15.         //This sets the number that the user is trying to guess to a number from 0-20
  16.         int answer = generator.nextInt(20);
  17.         //This while loop will run while the user hasn't won
  18.         while(userHasNotWon == true)
  19.         {
  20.             //Here, we ask the user from between 0-20 and assign it to an int called guess
  21.             System.out.print("Guess a number between 0 and 20: ");
  22.             int guess = input.nextInt();
  23.             //If the guess is over twenty then we tell the user
  24.             if(guess > 20) {
  25.                 System.out.println("The number can't be more than twenty!");
  26.             }
  27.             //If the guess is less than 0 then we tell the user
  28.             if(guess < 0) {
  29.                 System.out.println("The number can't be less than zero!");
  30.             }
  31.             //If the guess is in the right range then we check whether they were right or not
  32.             if(guess >= 0 && guess <= 20) {
  33.                 //If the guess is too high then we tell the user and increment the guess counter by one
  34.                 if (guess < answer) {
  35.                     System.out.println("Too low");
  36.                     guessCounter += 1;
  37.                 }
  38.                 //If the guess is too low then we tell the user and increment the guess counter by 1
  39.                 if (guess > answer) {
  40.                     System.out.println("Too high");
  41.                     guessCounter +=1;
  42.                 }
  43.                 /**If the user guesses the right number then we tell them they won and
  44.                 tell them how many times they had to guess**/
  45.                 if (guess == answer) {
  46.                     System.out.println("YOU WIN!!! And you guessed " + guessCounter + " times!!!");
  47.                     userHasNotWon = false;
  48.                 }
  49.             }
  50.         }
  51.     }
  52. }

4 comments:

  1. funny!i can't guess the right answer to what i programmed........good work on that....can you post more games please,so i can learn how the if,if else loop works and the boolean....thanks

    ReplyDelete
  2. Okay sure, I'm a bit busy but I'll try to get on it. Do you want any games in particular?

    ReplyDelete
  3. any game you think would be of help.thanks

    ReplyDelete
  4. @ Bidex Games Games Games - Why not attempt the X and O in Java
    @ Dreguin Good, I like this

    ReplyDelete