Results 1 to 9 of 9

Thread: Help

  1. #1
    PeterRook is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Question Help

    So I need to make this program and i got alot of it done already but im not very good with arrays and what not; can you guys help me. I know that some fo the code may be wrong but this is what it has to output.
    Please enter your name >> [Name here]

    I have generated a random number can you guess it?>>

    Your guess>> [number here]
    You are higher/lower than the random number
    Your guess>> [number here]
    You are higher/lower than the random number
    Your guess>> [number here]
    You are higher/lower than the random number
    Your guess>> [number here]
    You are higher/lower than the random number
    Your guess>> [number here]
    Correct [name] You have guessed the correct number [random number] in [amount of entries]

    The numbers you have guessed are:

    [numbers here]

  2. #2
    PeterRook is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    Code is as followes

    import containers.Input;
    import java.util.Random;

    public class Assesment
    {
    public static void main(String args[])
    {
    // max and min number
    int maxnum = 100;
    int minnum = 1;
    int guessnumber= 0;
    int count = 0;


    //random generator
    Random generator = new Random();
    guessnumber = generator.nextInt(maxnum);

    //Name
    String name;

    //array
    int array[] = new int[maxnum];


    //starting main code
    System.out.println("Please enter your name>> ");
    name = Input.readString();
    System.out.println("");
    System.out.println("I have generated a random number can you guess it?>>");

    //while
    while(guessnumber < 100)
    {
    System.out.println("Your guess >>");
    array[count] = Input.readInt();
    count++;
    }

    }
    }
    bold and underlined bit is an issue please help me?
    Last edited by PeterRook; 06-06-2011 at 12:17 PM.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    OK.
    And what have you done, and what are your errors/exceptions/problems?

    We're not going to write it for you.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    That's what happens when I leave a window open for 15 minutes...

    So, what is the problem you are having?
    You haven't actually said.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    A few remarks:

    1) you initialize guessnumber to 0 but a few lines further you set it to a random value; there is no need to initialize it to zero;
    2) why are you using an array? You don't need one: just read the user's guess and act accordingly (also keep a count because you want a limited number of guesses).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    PeterRook is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    Ive doen everything so far no errors yet except i dont know what to put in the while loop

  7. #7
    PeterRook is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    I need the array because at the end of the program its to print out the numbers the user has guessed

  8. #8
    PeterRook is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    Ok ive completed the code all except i have one problem - the

    while(guessnumber < 100)
    {
    System.out.println("Your guess >>");
    array[count] = Input.readInt();
    count++;
    }
    It works if it goes above 100 and the max number it should go to for a random number is 100 help anyone?

  9. #9
    PeterRook is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    Update -

    import containers.Input;
    import java.util.Random;

    public class Assesment
    {
    public static void main(String args[])
    {
    // max and min number
    int maxnum = 100;
    int minnum = 1;
    int guessnumber;
    int count = 0;


    //random generator
    Random generator = new Random();
    guessnumber = generator.nextInt(maxnum);

    //Name
    String name;

    //array
    int array[] = new int[maxnum];


    //starting main code
    System.out.println("Please enter your name>> ");
    name = Input.readString();
    System.out.println("");
    System.out.println("I have generated a random number can you guess it?>>");

    //while
    while(array[] == guessnumber)
    {
    System.out.println("Your guess >>");
    array[count] = Input.readInt();
    count++;

    if (count > guessnumber)
    {
    System.out.println("you are higher than you should be");
    }
    else
    {
    System.out.println("You are lower than you should be");
    }
    }
    }
    }

Posting Permissions

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