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: Help with the While in the Do-While

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Help with the While in the Do-While

    I have tried over and over again, but I can't seem to get this; especially with the while at the end. I think the problem is the "guess" right after the while, but I don't know. Any help would be appreciated. Thanks

    package practice;
    //imports
    import java.util.Scanner;
     
     
    public class DoWhile {
        public static void main(String[] args) {
     
            int num = (int) Math.random() * 101;
     
        do{
         System.out.println("Guess the number");
         Scanner scan = new Scanner (System.in);
         int guess = scan.nextInt();
     
          if (guess == num) {
            System.out.println("You are correct");
          }
     
          else if (guess < num) {
            System.out.println("You are high. Guess lower!");
          }
     
          else {
            System.out.println("You are low. Guess higher!");
     
          }
        }while (guess != num);
     
        }
     
    }

  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Help with the While in the Do-While

    You're declaring the variable guess inside of the do-while loop block. This means that when you leave this block, this variable will cease to exists. Unfortunately, the while loop condition testing is not part of the loop block. You must declare this variable before the do-while loop.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    maximus20895 (October 5th, 2010)

  4. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with the While in the Do-While

    Ah, that makes sense. So would I have to change the do or what?

    How would I declare the variable before the do-while loop?

    I just copied what my teacher did haha.

  5. #4
    Junior Member
    Join Date
    Oct 2010
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with the While in the Do-While

    Okay, so I just played around with it and this is what I got and it seemed to work.

    package practice;
    //imports
    import java.util.Scanner;
     
     
    public class DoWhile {
        public static void main(String[] args) {
            Scanner scan = new Scanner (System.in);
     
            int num = (int) Math.random() * 101;
            int guess = scan.nextInt();
     
     
        do{
         System.out.println("Guess the number");
     
          if (guess == num) {
            System.out.println("You are correct");
          }
     
          else if (guess < num) {
            System.out.println("You are high. Guess lower!");
          }
     
          else {
            System.out.println("You are low. Guess higher!");
     
          }
        }while (guess != num);
     
        }
     
    }

    I have only been doing java for a couple of weeks.

    I have another question if you don't mind. First, what is the difference between Scanner scan and Scanner input? Last where is the appropriate place to put "Scanner scan/input = new Scanner (System.in); I would assume you only put that line once since netbeans didn't like it when I did otherwise.

    Thanks, you have been a great help

  6. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Help with the While in the Do-While

    1)
    what is the difference between Scanner scan and Scanner input?
    The variable name. You can name your Scanner variable, and all variables, whatever you want.

    Now, if you are asking the difference between Scanning a File or Scanning the Runtime User Input, those are two different things. But the way to determine which of those you are doing is done when you construct the Scanner Object, not by the name you give it.

    2) Like all Objects and Variables, you want to create and initialize them before you plan on using them. And you should only create an Object/Variable with a specific name once. Which means, while sometimes allowed because of scope in JAVA, you should never create an Object/Variable with a name you have already given a different Object/Variable. With the statement:
    Scanner input = new Scanner(System.in);
    Scanner scan = new Scanner(System.in);
    You are create two different Scanner Objects. Since the System.in in the parameters tells the Scanner to read from the console, I'm not sure if you can actually attach two Scanners to it. Regardless, you should never attempt to create an Object/Variable twice, which is what you would be doing with the statement below:
    Scanner input = new Scanner(System.in);
    Scanner input = new Scanner(System.in);
    Instead, if you wanted to reconstruct your Scanner Object, you would say this:
    Scanner input = new Scanner(System.in);
    input = new Scanner(System.in);

    Tell me if that all makes sense.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  7. The Following User Says Thank You to aussiemcgr For This Useful Post:

    maximus20895 (October 6th, 2010)

  8. #6
    Junior Member
    Join Date
    Oct 2010
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with the While in the Do-While

    Yes, it all makes sense. I appreciate it