Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Help needed with java number guessing game, please

  • 16-08-2009 07: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, Registered Users 2, Paid Member Posts: 2,032 ✭✭✭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, Registered Users 2 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, Registered Users 2 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