我一直在試圖編寫這個程序幾個小時,所以請裸露在我身邊。我甚至分別分解了這些循環,但是一旦我把所有東西放在一起,它就無法工作,我需要它。循環語句邏輯
所以,讓我打破什麼,我需要每一個循環做,然後我得到什麼輸出:
1)第一個循環,我需要使用一個DO /時。在這個循環中,用戶被提示輸入一個單詞。如果用戶鍵入錯誤的單詞,則會收到錯誤消息:
無效!再試一次! 2次嘗試離開!
無效!再試一次! 1次嘗試離開!
對不起!你沒有更多的嘗試了!
這個輸出工作,只要錯一個字每一個進入時間,但如果1後輸入正確的單詞失敗的嘗試它仍然適用錯誤消息
2)在我的第二個循環( for循環),用戶被要求輸入「3 * 8 =」如果輸入了錯誤的數字3次,或者輸入了24,那麼循環的這部分工作正常。
問題出在24進入後的循環中。輸出如下:
謝謝你等等等等。如果您是贏家,我們會致電5555555555。 3 * 8 =其中3 * 8不應顯示。我意識到我可以休息一下;在此聲明之後,但說明明確指出我不能使用break命令。
正確的輸出應爲:謝謝你等等等等。如果您是贏家,我們會致電5555555555。
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int attempt = 2;
int answer = 24;
long phoneNumber = 0;
String firstName = "";
String lastName = "";
String wordOfTheDay = "";
System.out.printf("Enter the word of the day: ");
wordOfTheDay = input.nextLine();
if(wordOfTheDay.equals("tired"))
{
for(attempt = 2; attempt >= 0; --attempt)
{
System.out.print(" 3 * 8 = ");
answer = input.nextInt();
input.nextLine();
if(answer == 24)
{
System.out.printf("Please enter your first name, last name, and phone number (no dashes or spaces)\n" +"in a drawing for an all-expenses-paid vacation in the Bahamas: ");
firstName = input.next();
lastName = input.next();
phoneNumber = input.nextLong();
System.out.printf(
"Thank you %s %s. We'll call you at %d if you're a winner.",
firstName,
lastName,
+ phoneNumber);
}
else if(answer != 24)
{
if(attempt!=0)
{
System.out.printf("Invalid! Try Again! %d attempt(s) left!\n ", attempt);
continue;
}
else
{
System.out.print("Sorry! You have no more attempts left!");
}
}
}
}
else
{
do
{
System.out.printf("Invalid! Try Again! %d attempt(s) left!\n ", attempt);
--attempt;
System.out.printf("Enter the word of the day: ");
wordOfTheDay = input.nextLine();
} while (attempt >= 1);
if(attempt == 0)
{
System.out.print("Sorry! You have no more attempts left!");
}
}
}
我希望我說得夠清楚。
回顧一下,我需要解決我的問題/同時不讓我在失敗的嘗試後輸入正確的單詞。
此外,我需要擺脫3 * 8 =顯示後,用戶輸入正確的輸入。
謝謝!
唯一的檢查,你正在爲當天的正確詞彙在兩個循環之外,所以在你第一次檢查它時,不管你輸入什麼,你都不會再檢查它。 – DaveJohnston 2012-03-08 09:39:54
我應該重新將它作爲家庭作業嗎? – Juvanis 2012-03-08 09:50:58
你可以嘗試改善你的格式嗎?例如你的IDE會爲你做這個。 – 2012-03-08 09:55:33