請注意,儘管我對Java很滿意,但我並不是特別有天賦,也不知道所有術語,因此請用非常小的編碼術語和普通英語解釋您的答案,或者在使用它之後解釋行話的含義。此外,這是我第一次與Stackoverflow,所以讓我知道這是一個體面的問題,並給我一些指針。Do-while循環問題
我在高中時期參加AP計算機科學課。我們使用Java。我們最近被教導了do-while循環,並且我剛剛完成了使用do-while循環的「實驗室」,但是存在一個問題。
首先,讓我解釋一下實驗室。該程序生成一個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。但是,我沒有把它寫入程序,它似乎是自己做的。我使用了早期提到的打印compGen int的SOP,並且將其作爲我的第一次猜測輸入,並且完美運行。 if語句塊中的所有內容都可以完美執行並正確地打印所有內容。但是,當我做了第二次猜測,第三次猜測或任何不是第一次的猜測時,沒有打印任何內容。看到下面的代碼並運行它。我不相信這應該有所作爲,但我使用的IDE是Eclipse,因此是包聲明。請幫忙。
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語句並重新輸入(我知道它不會做任何事情,但我必須嘗試)。這個問題對我來說毫無意義。沒有任何錯誤或任何東西彈出,沒有任何東西彈出在控制檯上,這讓我感到害怕。提前致謝。
感謝您提出減少問題的建議。這是我的第一篇文章,並希望說清楚,我沒有這個網站的經驗。現在到你的答案,我讚賞按積分分組,這絕對有幫助。下面的用戶指出,如果提交了正確的答案,則由於while條件將是錯誤的,因此do-while將終止。你的答案不會有同樣的問題嗎?即使'nextInt()'在塊的頂部? – Ungeheuer 2014-10-17 00:56:32
不,因爲用戶輸入在if-elseif-else進入之前被讀取。因此,輸入始終在相同的迭代過程中完成,並在之後結束。 – Felk 2014-10-17 01:01:34
哦!現在我明白了!通過在頂部輸入用戶輸入,答案將過濾到if/else-if塊,保留該值並允許'userGen == compGen'代碼正確執行。而且我仍然可以得到「猜得太高」的代碼,因爲我在循環中得到了int。你先生非常聰明。 :) – Ungeheuer 2014-10-17 01:02:26