Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Help needed with java number guessing game, please

Options
  • 16-08-2009 7:31pm
    #1
    Closed Accounts Posts: 4


    Hi All,
    I'm looking for any help anyone could give me for the problem below, I'm at the early stages of a Java programming course and have run into problems. I have tried debugging the errors and re-started a couple of times, but think I'm going about it completly wrong.The out-line of the program is:

    Write a program which asks the user for 1 integer number between 1 and 10.
    Get the computer to randomly generate a number between 1 and 10
    If your number == it's number, print "you have won"
    If your number < computer's number, print "Too low"
    if your number > computer's number, print "Too High"
    Print out both numbers

    I have'nt posted any errors as there is always others leading me to believe I have made a balls of the whole thing.Also the aim is to use the java.util.Random; function.

    import java.util.Random; //call the random number generator
    public class Game
    {
    static Random generator = new Random();
    public static void main (int a)

    {
    int b;
    b = getRandomNumber(11); //no's between 0 - 10

    if (value == a)
    {
    System.out.println ("You have WON!");
    }
    if (value < a)
    {
    System.out.println ("Too Low");
    }
    if (value > a)
    {
    System.out.println ("Too High");
    }

    }

    private static int getRandomNumber(int b)
    {
    Random generator = new Random();
    int value;

    value = generator.nextInt( b );

    }
    } //end class

    Please keep it as basic as possible as I don't want to be hassling people for explanations of their hopefully helpful posts

    Thanks,
    Marzy


Comments

  • Registered Users Posts: 1,998 ✭✭✭lynchie


    Firstly you need to read up on passing in command line arguments at
    http://java.sun.com/docs/books/tutorial/getStarted/application/index.html.

    Secondly your getRandomNumber method doesnt return anything.

    Thirdly you have a staic instance of Random that does nothing either.


  • Registered Users Posts: 2,297 ✭✭✭Ri_Nollaig


    Few things.

    I'd say you need to check up more on scope, you have declared a variable "value" in getNumberFromUser(), this would not be visible in the main. It would need to be a global and then a static too for that to work.

    change getRandomNumber(int b) to :
    private static int getRandomNumber(int limit) {
       Random generator = new Random();
       return generator.nextInt(limit);
    }
    
    now just assign b (perhaps a better name?) to the result of that.

    Also, your not actually reading in a number anywhere. You look like your trying to use arguments but they are stored as a String Array. Judging from the description ull need to use something like System.in

    add this method:
    import java.io.*;
    
    ...
    
    private static int getNumberFromUser() {
       int input -1;
       System.out.println("Enter a number between 1 and 10");
       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       try {
          String userInput = br.readLine();
          try {
             input = Integer.parseInt(userInput);
             if(input < 1 || input > 10) {
                input = -1;
             }
          } catch (Exception e) {
             e.printStackTrace(); 
          }
       } catch (IOException e) {
          e.printStackTrace();
       }
       return input;
    }
    
    

    have your main assign 'value' to the result of that.

    I'm sure you can figure out how to print the two numbers.
    Also get rid of the unused variables...


  • Closed Accounts Posts: 4 marzy


    Thanks for the help lads, hope to get a go at this over the weekend (if the kids let me)

    Marzy


  • Registered Users Posts: 3,945 ✭✭✭Anima


    I'd use the Scanner class to read in text. Its very easy to use.

    [PHP]
    class HM1
    {
    psvm{ // not writing it all out, its the main function
    Scanner input = new Scanner( System.in );
    Random rand = new Random( System.currentTimeMillis() ); // will seed the random generator so you dont get the same result every time you run the program
    int num1;

    num1 = input.nextInt(); // will sit on this line til you enter an int

    if( num1 == (rand.nextInt(11)+1) ) { // rand.nextInt(10) on its own will give you a number between (0 and 9)
    System.out.println("You a winna! Ha! Ha! Ha!");
    }
    }
    }
    [/PHP]


Advertisement