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 6 of 6

Thread: Some Quick Help PLZ

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

    Default System.out.println problem

    I'm new to this and trying to teach myself (I'm only 13). So I wrote this code and have everything working except, I want it too display "number out of range, try again" message only when you enter a number higher than 100. Right now it displays "Too high, try again" and "number out of range, try again". How do i change it to display "number out of range, try again" only? Also how do i get to display "Guess a number between 1 and 100" only once and not every time you try and guess the number. Any help will be greatly appreciated.





    import java.util.Scanner;

    public class randomNumber {

    public static void main(String[] args) {

    int randomNumber;

    int guessCount;

    int tooHigh;

    randomNumber = (int)(100 * Math.random()) + 1;
    guessCount = 0;
    tooHigh = 101;

    Scanner keyboard = new Scanner(System.in);

    int guess;



    do {

    System.out.println("Guess a number between 1 and 100");

    System.out.println("Enter your guess");

    guess = keyboard.nextInt();
    guessCount++;


    if (guess == randomNumber) {
    System.out

    .println("Congratulations your guess was correct!" +
    " It took " + guessCount + " guesses.");
    }

    else if (guess < randomNumber) {
    System.out

    .println("Too low, try again .");
    }

    else if (guess > randomNumber) {
    System.out

    .println("Too high, guess again.");
    }

    if (guess >= tooHigh) {
    System.out

    .println("Number out of range, try again.");
    }



    } while (guess != randomNumber);


    }
    }


    I ran it through NetBeans IDE to show you my problems.
    run:
    Guess a number between 1 and 100
    Enter your guess
    100
    Too high, guess again.
    Guess a number between 1 and 100
    Enter your guess
    101
    Too high, guess again.
    Number out of range, try again.
    Guess a number between 1 and 100
    Enter your guess
    50
    Too high, guess again.
    Guess a number between 1 and 100
    Enter your guess
    30
    Too low, try again .
    Guess a number between 1 and 100
    Enter your guess
    40
    Too high, guess again.
    Guess a number between 1 and 100
    Enter your guess
    33
    Too low, try again .
    Guess a number between 1 and 100
    Enter your guess
    35
    Too high, guess again.
    Guess a number between 1 and 100
    Enter your guess
    34
    Congratulations your guess was correct! It took 8 guesses
    BUILD SUCCESSFUL (total time: 50 seconds)
    Last edited by tmayo47; September 23rd, 2012 at 12:19 AM.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Some Quick Help PLZ

    () Typing words like "Quick" or "Urgent" does not make me decide to check the forum any sooner, wastes readers' time, and causes people to not read your post. So probably not a good title to get help with a problem.

    ()
    I'm new to this and trying to teach myself
    :-) That is the fun way

    ()Please put [code=java] before your code and [/code] after your code to make it all colorful for old eyes to read it.

    ()Your questions suggest you skipped the first steps of problem solving. I like to get a clear understanding of the problem I am to solve in code. Then come up with a way to solve that problem.
    In your case you want to get a number from the user until the guess is correct. That would make one big loop. Inside that loop you have other tasks to do. The things you want to see only when the user makes a guess will be inside that loop.
    The important thing to see here is how your solution-on-paper will later translate into code that produces a loop to do some tasks, but not others. Then you will already know where to place things in your code before you write the code.
    Best advice I can think of, get a well organized plan before writing any code.

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

    Default Re: Some Quick Help PLZ

    Ok, so i did a little flowchart this morning and figured out my problem for Guess a number and enter a number. I'm suck on the out of range for numbers. I'm going to do some reading but if you can point me in the right direction that would be great. Also, sorry about the thread title it was late and i was getting a little frustrated with myself.

     
     
     
    import java.util.Scanner;
     
     public class randomNumber {
     
          public static void main(String[] args) {
     
                int randomNumber;
     
                int guessCount;
     
                int tooHigh;
     
                randomNumber = (int)(100 * Math.random()) + 1;   
                guessCount = 0;
                tooHigh = 101;
     
                Scanner keyboard = new Scanner(System.in);
     
                int guess;
     
                System.out.println("Guess a number between 1 and 100");
                System.out.println("Enter your guess");
     
                do {
     
     
     
                      guess = keyboard.nextInt();
                      guessCount++;
     
     
                      if (guess == randomNumber) {
                        System.out
     
                                .println("Congratulations your guess was correct!" + 
                                " It took " + guessCount + " guesses.");
                    }
     
                      else if (guess < randomNumber) {
                        System.out
     
                                   .println("Too low, try again .");
                    }
     
                      else if (guess > randomNumber) {
                        System.out
     
                                   .println("Too high, guess again.");
                    }
     
                      if (guess >= tooHigh) {
                          System.out
     
                                 .println("Number out of range, try again."); 
                      }
     
     
     
                } while (guess != randomNumber);
     
     
                          }
                 }

  4. #4
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: Some Quick Help PLZ

    I would suggest changing your if statements. I think you should read up on if and else if statements to understand why it's doing this. Here's a quick tutorial for if statements.

    The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)

    What is the statement doing? If this condition is true, do this.. else if, do this.

    *hint* What do if statements do after they end?

  5. The Following User Says Thank You to Actinistia For This Useful Post:

    tmayo47 (September 23rd, 2012)

  6. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Some Quick Help PLZ

    Quote Originally Posted by tmayo47 View Post
    Ok, so i did a little flowchart this morning ... I'm suck on the out of range for numbers...
    What did your flow chart say about when to check if the value is in range? That place should correspond to a place in the code.

    Something to think about, a relationship among the numbers...

    As a rule: tooLow < randomNumber < tooHigh ...or randomNumber is not in range, right?

    Consider: tooLow < lowGuess <= randomNumber <= highGuess < tooHigh

  7. #6
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Some Quick Help PLZ

    Thanks for the video that helped. Also i figured it out thank you for help. Now time to try write another one with arrays.

Similar Threads

  1. A quick hello
    By Bitbot in forum Member Introductions
    Replies: 0
    Last Post: June 10th, 2012, 05:40 PM
  2. quick question, please HELP!
    By Nemus in forum What's Wrong With My Code?
    Replies: 14
    Last Post: September 29th, 2011, 01:23 PM
  3. Quick Question
    By sird00dguyman in forum Java Theory & Questions
    Replies: 2
    Last Post: August 4th, 2011, 06:14 PM
  4. Another Quick Question
    By Jacksontbh in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 1st, 2011, 07:18 AM
  5. Need some serious quick help. :/
    By aanders5 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 23rd, 2010, 06:05 PM