Results 1 to 3 of 3
  1. #1
    eob13 is offline Member
    Join Date
    Oct 2013
    Posts
    1
    Rep Power
    0

    Default Help?? Guess number game

    Hi,

    I'm new to java, have been learning for 4 weeks. Ive an assignment tomorrow and have been given the following clues which the exam will contain; comments, user input, constants, if else statement (more than 2), loop (only break if you get the value you want), wrong (please enter correct number), correct (do you want to play again).

    I could be wrong but going by the above i think it could be a guessing game? I have got the following, but it needs to be changed to contain loops / constants etc. If anyone could help would be greatly appreciated, thanks in advance :

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

    /**
    * @author endaobrien1313
    *
    */
    public class HighLowGuess {

    /**
    * @param args
    */
    public static void main(String[] args) {

    //Get scanner
    Scanner scanner = new Scanner (System.in);
    Random generator = new Random ();

    // Declaring variables
    int num2;
    int guessCount;
    boolean playAgain = true;

    while (playAgain) {
    guessCount = 0;
    int num1 = generator.nextInt(101) + 1;
    // Tell user to guess a number
    System.out.println("Guess a number between 1 and 100. \nEnter your guess (0 to quit): ");
    // Get users input
    num2 = scanner.nextInt();
    scanner.nextLine();

    //Check if
    while (num2 != 0) {
    if (num2 < num1) {
    System.out.println("You guess was too low. \nGuess again (0 to quit): ");
    num2 = scanner.nextInt();
    scanner.nextLine();
    guessCount++;
    }
    else if (num2 > num1) {
    System.out.println("Your guess was too high. \nGuess again (0 to quit): ");
    num2 = scanner.nextInt();
    scanner.nextLine();
    guessCount++;
    }
    else {
    System.out.println("Your guess was correct!");
    guessCount++;
    System.out.println("Number of guesses: " + guessCount);
    num2 = 0;
    }
    }
    System.out.println("Would you like to play again? (y/n): ");
    String input = scanner.nextLine();
    if (input.equalsIgnoreCase("n")) {
    playAgain = false;

    }
    }
    scanner.close();

    }

    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: Help?? Guess number game

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Help?? Guess number game

    Quote Originally Posted by eob13 View Post
    If anyone could help would be greatly appreciated, thanks in advance :
    What help did you want? The best way to get help is to ask a specific question.

Similar Threads

  1. Need help with a Basic > guess a number Game
    By John2find in forum New To Java
    Replies: 8
    Last Post: 07-26-2013, 02:48 PM
  2. my guess game problem
    By nonybrighto in forum New To Java
    Replies: 3
    Last Post: 12-28-2012, 11:01 PM
  3. guess game GUI
    By tomandhisjones in forum New To Java
    Replies: 4
    Last Post: 04-04-2011, 06:30 AM
  4. Replies: 3
    Last Post: 12-20-2009, 03:22 PM
  5. guess number game
    By mistah in forum New To Java
    Replies: 10
    Last Post: 11-23-2008, 03:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •