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
Code :
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);
}
}
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.
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.
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.
Code :
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 :)
Re: Help with the While in the Do-While
1)
Quote:
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:
Code java:
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:
Code java:
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:
Code java:
Scanner input = new Scanner(System.in);
input = new Scanner(System.in);
Tell me if that all makes sense.
Re: Help with the While in the Do-While
Yes, it all makes sense. I appreciate it :)