Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Complete Noob -- A little help would be appreciated...

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Complete Noob -- A little help would be appreciated...

    I'm seriously confused with the Random class in relation to the following question:

    Write a program to prompt the user for three integers from 1 to 3. Using random numbers, print which number(s) are guessed correctly. Allow the user to continue to guess by inputting any incorrect number(s) until the user inputs an ‘n’ to quit the program, or the program displays “CONGRATULATIONS, ALL THREE NUMBERS WERE GUESSED CORRECTLY! THE NUMBERS WERE: 2, 2, 1.” The logic for the user would look like:

    You may input an ‘n’ to quit the program at any time.
    Please enter number 1: 2
    Please enter number 2: 1
    Please enter number 3: 2

    Number 1 is correct.

    Is the following code randomly generating a number between 0-3 and, if so, what would the next step be to compare the randomly generated number to the user input? Here is the code:

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

    public class Random
    {
    public static void main(String[] args)
    {
    Scanner keyboard = new Scanner (System.in);

    int number1;
    int number2;
    int number3;
    int userAnswer;

    Random randomNumbers = new Random()

    number1 = randomNumbers.nextInt(3);
    number2 = randomNumbers.nextInt(3);
    number3 = randomNumbers.nextInt(3);

    System.out.println("Please guess number 1. The number must be within zero and three. ";
    userAnswer = keyboard.nextInt();

    ETC...

    Any help is appreciated.

    E


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Complete Noob -- A little help would be appreciated...

    Firstly, please format your code using highlight tags. See my signature.

    The <Random>.nextInt(int number) methods returns integers between 0 and number - 1, inclusive. So, for example, if you used <Random>.nextInt(7) you would get a random integer ranging from 0 to 6. Similarly, if you want numbers from 2 to 4, not starting at 0, you could use <Random>.nextInt(3) + 2.

    If that doesn't help, use some System.println()'s to help display what your random numbers are.

    System.out.println("Please guess number 1. The number must be within zero and three. ";
    userAnswer = keyboard.nextInt();
    This looks alright, but think: what if the user enters a number less than one or greater than three, or maybe not even a number at all? You should probably use some input validation and a while loop to make sure that the user doesn't play any dirty tricks to crash your program.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

Similar Threads

  1. [SOLVED] I dont know how to complete this code?
    By jwb4291 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 8th, 2017, 11:11 PM
  2. Complete newbie keybinding
    By Peperono in forum AWT / Java Swing
    Replies: 4
    Last Post: November 11th, 2011, 06:05 PM
  3. A little help need to complete my tutorial question
    By hengchuen in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 17th, 2011, 07:51 AM
  4. How to complete code
    By Shay in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 5th, 2010, 01:59 PM
  5. How to delete and add elements to an array dynamically?
    By k_w_r2007 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 21st, 2009, 11:31 AM