[英]Issue with do-while loop
請注意,雖然我對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語句並重新輸入它(我知道它不會做任何事情,但我必須嘗試)。 這個問題對我沒有意義。 沒有錯誤或任何彈出的東西,控制台上沒有任何東西彈出讓我有點害怕。 提前致謝。
首先,你在這里放了很多文字。 也許嘗試下次作為建議盡量減少問題;)否則一切都很好
對你的問題。 讓我最小化你的代碼,然后向你解釋,會發生什么。
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()
讀取,只有一個作為循環內的第一件事:
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
循環來完成
您在循環期間設置用戶輸入,然后檢查它。 嘗試將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
}
一旦程序到達循環內部代碼的末尾,就會檢查while
條件。 所以假設他們輸錯了數字; 該程序說它太低或太高,然后要求另一個數字:
userGen = userInput.nextInt();//new guess
現在假設這個新數字是正確的。 程序完成if
語句,然后到循環結束。 然后,在那時, userGen
等於compGen
。 所以while
條件失敗了,程序立即退出循環,而沒有得到打印結果的代碼。
解決這個問題的一種方法是移動userGen == compGen
的邏輯,它在循環外部 - 也就是在循環結束之后打印結果。 這樣,只要退出循環就會執行它。 請注意,當您退出循環時,我們知道userGen == compGen
,因為如果不是,則循環將返回。
假設計算機生成的數字是3,你猜是5 5> 3,所以if(userGen> 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)。 您的代碼永遠不會有機會打印正確的消息,因為循環在它發生之前退出。 希望有所幫助
每次用戶輸入后都不會檢查您的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塊。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.