请注意,虽然我对Java感到满意,但我并不是特别有天赋,也不知道所有的行话,所以请用很少的编码器术语和正常的英语来解释你的答案,或解释一下术语的含义你用吧。此外,这是我第一次使用Stackoverflow,所以让我知道这是一个不错的问题,并给我一些指示。
我正在上高中读AP计算机科学课。我们使用Java。我们最近教过do-while循环,我刚刚完成了使用do-while循环的“lab”,但是,有一个问题。
首先,让我解释一下实验室。程序生成一个1-10之间的随机整数,用户必须猜测(猜测是使用扫描仪存储为int),有几个整数值跟踪猜测数量,多少猜测大于计算机整数,以及有多少太低了。当你查看我的代码时,你会注意到我有一个System.out.println(compGen);//compGen is the computer generated int
。目的是测试代码。
问题出在比较userGen(用户猜测)和compGen的if语句中。
if(userGen==compGen)
{
//do a lot of stuff
}
在这个if语句中,如果用户猜不到一次,它就不会打印我写的正确的SOP。但是,我没有把这个写入程序,它似乎是自己做的。我使用了早期提到的SOP,其中打印了compGen int,我输入的是我的第一个猜测,它完美地工作。 if语句块中的所有内容都完美执行并正确打印所有内容。然而,当我把它作为我的第二个猜测,第三个猜测或任何不是第一个的猜测时,打印出了NOTHING。请参阅下面的代码并运行它。我不相信这会有所作为,但我使用的IDE是Eclipse,因此是package语句。请帮忙。
package Chapter_3.Lab03_Chapter3;
import java.util.*;
public class Guess
{
public static void main(String[] args)
{
Scanner userInput = new Scanner(System.in);//Scanner
int compGen = (int)(Math.random()* 10 + 1);//compGen is computer number
System.out.println(compGen); //USED TO VERIFY FAILURE. VALUE WAS ENTERED TO TEST CODE
int guessTrack = 0;//tracks number of guesses
int tooHighTrack = 0;//CREATING INTS TO TRACK STUFF
int tooLowTrack = 0;
System.out.println("Welcome to the integer guessing game that everyoone loves!");//PROMPT
System.out.println("Please enter your guess for the integer. Remeber, it is between one and ten.");//GREETING
int userGen = userInput.nextInt();//USER GUESS
do
{
guessTrack++;//Increase guess value
if(userGen > compGen)//checks user value in relation to computer generated int
{
System.out.println("Try again! Your guess was too high!");//inform user of bad guess
userGen = userInput.nextInt();//new guess
tooHighTrack++;//if guess is too high, this int tracker increases
}
else if(userGen < compGen)//checks user value in relation to computer generated int
{
System.out.println("Try again! Your guess was too low!");//inform user of guess
userGen = userInput.nextInt();//new guess
tooLowTrack++;//increases if user guess is too low
}
else if(userGen==compGen)//if both values are equivalent, execute THIS IS THE PROBLEM STATEMENT!!
{
System.out.println("Great job! You guessed the right number!");//congratulate
if(guessTrack>1)
{
System.out.println("It took you: "+guessTrack+" guess to get the right answer.");//print guess tracked int
}
else
{
System.out.println("It took you: "+guessTrack+" guesses to get the right answer.");//print guess tracked int
}
System.out.println(tooHighTrack +" guesses were too high and "+ tooLowTrack+ " were too low.");//print how many guess were too big or too low
System.out.println("HELLO"); //Used to verify failure of code
userInput.close();//close scanner object
}
}
while (userGen != compGen);//condition to be ultimately checked
}
}
我无法弄清楚出了什么问题。有一次我删除了整个if语句并重新输入它(我知道它不会做任何事情,但我必须尝试)。这个问题对我没有意义。没有错误或任何弹出的东西,控制台上没有任何东西弹出让我有点害怕。提前谢谢。
答案 0 :(得分:5)
首先,你在这里放了很多文字。也许尝试下次作为建议尽量减少问题;)否则一切都很好
解决你的问题。让我尽量减少您的代码,然后向您解释会发生什么。
<强> 1。守则
int val = scanner.nextInt();
do {
if (val < 5) {
// too low
val = scanner.nextInt();
} else if (val > 5) {
// too high
val = scanner.nextInt();
} else {
// correct
// THIS CODE DOESN'T RUN?!
}
} while (val != 5);
<强> 2。你的代码做了什么?
您在循环之前阅读了第一个数字。没关系。然后,输入if-elseif-else语句。注意,一旦进入其中一个块,其他块就不会被执行。 现在的问题是,您在if-elseif中读取了下一个用户输入!程序读取下一个值并留下整个if-elseif-else。您的代码不会执行,因为循环然后在下一次迭代之前结束,因此正确的用户输入不会通过if-elseif-else。
第3。解决方案
删除所有nextInt()
次读取,只需要一个 in 循环:
int val;
do {
val = scanner.nextInt();
if (val < 5) {
// too low
} else if (val > 5) {
// too high
} else {
// correct
// THIS CODE RUNS NOW!
}
} while (val != 5);
这样的事情,在检查循环条件之前需要至少执行一次的结构通常使用do while
循环而不是while
循环来完成
答案 1 :(得分:3)
您在循环期间设置用户输入,然后进行检查。尝试将else if(userGen==compGen)
块的主体移动到循环之后,如下所示:
public static void main(String[] args)
{
Scanner userInput = new Scanner(System.in);//Scanner
int compGen = (int)(Math.random()* 10 + 1);//compGen is computer number
System.out.println(compGen); //USED TO VERIFY FAILURE. VALUE WAS ENTERED TO TEST CODE
int guessTrack = 0;//tracks number of guesses
int tooHighTrack = 0;//CREATING INTS TO TRACK STUFF
int tooLowTrack = 0;
System.out.println("Welcome to the integer guessing game that everyoone loves!");//PROMPT
System.out.println("Please enter your guess for the integer. Remeber, it is between one and ten.");//GREETING
int userGen = userInput.nextInt();//USER GUESS
do
{
guessTrack++;//Increase guess value
if(userGen > compGen)//checks user value in relation to computer generated int
{
System.out.println("Try again! Your guess was too high!");//inform user of bad guess
userGen = userInput.nextInt();//new guess
tooHighTrack++;//if guess is too high, this int tracker increases
}
else if(userGen < compGen)//checks user value in relation to computer generated int
{
System.out.println("Try again! Your guess was too low!");//inform user of guess
userGen = userInput.nextInt();//new guess
tooLowTrack++;//increases if user guess is too low
}
}
while (userGen != compGen);//condition to be ultimately checked
//The numbers have matched since it exited the loop.
System.out.println("Great job! You guessed the right number!");//congratulate
if(guessTrack>1)
{
System.out.println("It took you: "+guessTrack+" guess to get the right answer.");//print guess tracked int
}
else
{
System.out.println("It took you: "+guessTrack+" guesses to get the right answer.");//print guess tracked int
}
System.out.println(tooHighTrack +" guesses were too high and "+ tooLowTrack+ " were too low.");//print how many guess were too big or too low
System.out.println("HELLO"); //Used to verify failure of code
userInput.close();//close scanner object
}
答案 2 :(得分:1)
一旦程序到达循环内部代码的末尾,就会检查while
条件。所以假设他们输错了数字;程序说它太低或太高,然后要求另一个数字:
userGen = userInput.nextInt();//new guess
现在假设这个新号码是正确的。程序完成您的if
语句,然后到达循环结束。然后,此时userGen
等于compGen
。因此while
条件失败,程序立即退出循环,而没有得到打印结果的代码。
解决问题的一种方法是移动userGen == compGen
的逻辑,在循环之外 - 也就是在循环结束之后打印结果。这样,只要退出循环就会执行它。请注意,当您退出循环时,我们知道userGen == compGen
,因为如果它不是,则循环将返回。
答案 3 :(得分:1)
让我们说计算机生成的数字是3,你猜5。 5> 3,所以if(userGen&gt; compGen)语句执行:
System.out.println("Try again! Your guess was too high!");//inform user of bad guess
userGen = userInput.nextInt();//new guess
tooHighTrack++;//if guess is too high, this int tracker increases
你打印信息,得到一个新的猜测,然后增加你的计数器...但是当你得到新的猜测,说它是正确的答案3,userGen现在等于CompGen(两者都是3),现在 在评估条件时:
while(userGen!= compGen)
现在这是假的,因为userGen == compGen(两者都是3)。你的代码永远不会有机会 打印正确的消息,因为循环在它发生之前退出。希望有所帮助
答案 4 :(得分:0)
每次用户输入后都没有检查您的userGen。 问题是你在else-if块中进行了检查,它会在它再次循环之前检查while语句的结束。
如果你改变了
else if(userGen==compGen)
到
if(userGen==compGen)
然后因为它不是if-else块的一部分,所以每次输入后都会检查它(在检查while条件之前)
或者,您可以将用户输入移动到do-while块的开头,如下所示:
package Chapter_3.Lab03_Chapter3;
import java.util.*;
public class Guess
{
public static void main(String[] args)
{
Scanner userInput = new Scanner(System.in);//Scanner
int compGen = (int)(Math.random()* 10 + 1);//compGen is computer number
System.out.println(compGen); //USED TO VERIFY FAILURE. VALUE WAS ENTERED TO TEST CODE
int guessTrack = 0;//tracks number of guesses
int tooHighTrack = 0;//CREATING INTS TO TRACK STUFF
int tooLowTrack = 0;
System.out.println("Welcome to the integer guessing game that everyoone loves!");//PROMPT
System.out.println("Please enter your guess for the integer. Remeber, it is between one and ten.");//GREETING
int userGen = -1;//USER GUESS
do
{
userGen = userInput.nextInt();
guessTrack++;//Increase guess value
if(userGen > compGen)//checks user value in relation to computer generated int
{
System.out.println("Try again! Your guess was too high!");//inform user of bad guess
userGen = userInput.nextInt();//new guess
tooHighTrack++;//if guess is too high, this int tracker increases
}
else if(userGen < compGen)//checks user value in relation to computer generated int
{
System.out.println("Try again! Your guess was too low!");//inform user of guess
userGen = userInput.nextInt();//new guess
tooLowTrack++;//increases if user guess is too low
}
else if(userGen==compGen)//if both values are equivalent, execute THIS IS THE PROBLEM STATEMENT!!
{
System.out.println("Great job! You guessed the right number!");//congratulate
if(guessTrack>1)
{
System.out.println("It took you: "+guessTrack+" guess to get the right answer.");//print guess tracked int
}
else
{
System.out.println("It took you: "+guessTrack+" guesses to get the right answer.");//print guess tracked int
}
System.out.println(tooHighTrack +" guesses were too high and "+ tooLowTrack+ " were too low.");//print how many guess were too big or too low
System.out.println("HELLO"); //Used to verify failure of code
userInput.close();//close scanner object
}
}
while (userGen != compGen);//condition to be ultimately checked
}
}
每次用户在检查do-while条件之前输入数据时,都会检查if-else块。