2012-03-08 164 views
2

我一直在試圖編寫這個程序幾個小時,所以請裸露在我身邊。我甚至分別分解了這些循環,但是一旦我把所有東西放在一起,它就無法工作,我需要它。循環語句邏輯

所以,讓我打破什麼,我需要每一個循環做,然後我得到什麼輸出:

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 =顯示後,用戶輸入正確的輸入。

謝謝!

+0

唯一的檢查,你正在爲當天的正確詞彙在兩個循環之外,所以在你第一次檢查它時,不管你輸入什麼,你都不會再檢查它。 – DaveJohnston 2012-03-08 09:39:54

+0

我應該重新將它作爲家庭作業嗎? – Juvanis 2012-03-08 09:50:58

+0

你可以嘗試改善你的格式嗎?例如你的IDE會爲你做這個。 – 2012-03-08 09:55:33

回答

1

通常情況下,你可以使用break聲明,但不允許你設置attempt = -1將有同樣的效果:

if(answer == 24) 
{ 
    ... 
    attempt = -1; // An ugly 'break' 
} 

編輯:

移動的do { } while();之前if檢查:

// These two lines of code are no longer required. 
    //System.out.printf("Enter the word of the day: "); 
    //wordOfTheDay = input.nextLine(); 

    do 
    { 
     System.out.printf("Enter the word of the day: "); 
     wordOfTheDay = input.nextLine(); 

     if(!wordOfTheDay.equals("tired")) 
     { 
      System.out.printf(
       "Invalid! Try Again! %d attempt(s) left!\n ", --attempt); 
     } 
     else 
     { 
      attempt = 0; // Another ugly 'break' 
     } 
    } while (attempt >= 1); 


    if(wordOfTheDay.equals("tired")) 
    { 
    } 
    // Remove else branch as not required. 
+0

這固定了我的第二個輸出錯誤。我不知道她是否會接受這種不間斷的方式,但我會找到這樣或那樣的方式。感謝您的幫助,現在找出第一個輸出錯誤。 – Abweichung 2012-03-08 10:11:04

+0

移動'do {} while();''獲得'wordOfTheDay'到'if(wordOfTheDay.equals(「tired」))''之前。 – hmjd 2012-03-08 10:20:46

+0

我這樣做了,但是錯誤信息提示並不是每次都顯示出來,在最後一次失敗的嘗試中需要 說聲對不起!你沒有更多的嘗試了!「例如,我會輸入」tir「,它會提示輸入一天中的字詞而不是無效!然後再次提示。 – Abweichung 2012-03-08 10:31:22

0

當您得到正確的結果時,您需要退出循環。這是使用break聲明完成的:

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); 
    break; 
    // that's the break statement 
} 
+0

根據問題,他不允許使用'break'。 – hmjd 2012-03-08 09:42:33

+0

不幸的是我不能使用break語句。這些指令說: 代碼System.exit(0)只在main()的末尾有一次,這意味着你將不得不將循環控制變量設置爲一個強制終止do/while和for循環的值用戶輸入的提示是正確的。你不能使用break語句。 – Abweichung 2012-03-08 09:43:34

0

好吧,顯然你必須打破外層循環。

如果在if區塊中有正確答案(即'answer == 24'),則將某個布爾變量'haveAnswer'設置爲'true'(初始化爲'false'並在' System.out中。打印(「3 * 8 =」);'對於「如果(haveAnswer){打破;}」

enter code here`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")) 
{ 
    boolean haveAnswer = false; 
    for(attempt = 2; attempt >= 0; --attempt) 
     { 
     if (haveAnswer) 
      break; 
     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); 

      haveAnswer = true; 
      break; 
     } 

     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!"); 
    } 
} 

System.exit(0); 

} 
} 
1

除非我失去了一些東西,你都在等待第一個輸入,如果它是錯的,你是在一個do-while循環去然後永不檢查再次如果輸入是正確的。

你必須移動,如果do-while循環中:

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(); 
    do { 
     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 { 

      System.out.printf("Invalid! Try Again! %d attempt(s) left!\n ", 
        attempt); 
      System.out.printf("Enter the word of the day: "); 
      wordOfTheDay = input.nextLine(); 
      if (!wordOfTheDay.equals("tired")) --attempt; 

     } 

    } while (attempt >= 1); 

    if (attempt == 0) { 

     System.out.print("Sorry! You have no more attempts left!"); 
    } 

    System.exit(0); 

} 
+0

我剛把它複製到JCreator的測試類中。這隻有在第一次嘗試錯誤時纔有效。我可以輸入一次錯誤地累進這個詞,它會提示3 * 8 =,但如果我錯誤地輸入了兩次,然後在最後一次嘗試中輸入正確,它仍然會給我「」對不起! 「 – Abweichung 2012-03-08 10:05:20

+0

是的,因爲當你到達最後一次嘗試時,'attempt'爲0,所以它檢查while狀態並退出。 – Tudor 2012-03-08 10:39:34

+0

我已經編輯了else分支,其中嘗試'遞減,現在應該是正確的 – Tudor 2012-03-08 10:48:22

0

此代碼不完整,但它具有您需要的ide。試試,。

布爾firstTry = true; 布爾keepLo​​oping = true;

do 
    { 
    if(!firstTry){ 
      System.out.printf("Invalid! Try Again! %d attempt(s) left!\n ", attempt); 
    } 

     System.out.printf("Enter the word of the day: "); 
     wordOfTheDay = input.nextLine(); 
    if(wordOfTheDay.equals("hey!")){ 
     keepLooping = false; 
    } 
    --attempt; 
    } while (attempt >= 1 && keepLooping); 
0

你最好用戶休息一下跳出循環。否則循環會繼續,打印「3 * 8 =」並等待你的輸入。 如果你不想使用休息時間,那麼使用attemptp = -1也是有意義的。

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(); 
     attempt = -1; 
     System.out.printf("Thank you %s %s. We'll call you at %d if you're a winner.", firstName, lastName,        + phoneNumber); 

     } 
2

我想你正在嘗試這樣做。

import java.util.Scanner; 

public class Main { 
    public static void main(String[] args) { 

     Scanner input = new Scanner(System.in); 

     int answer = 0; 
     long phoneNumber = 0; 
     String firstName = ""; 
     String lastName = ""; 
     String wordOfTheDay = ""; 

     int mainMenuAttempt = 3; 
     do { 
      System.out.printf("Enter the word of the day: "); 
      wordOfTheDay = input.nextLine(); 

      if (wordOfTheDay.equals("tired")) { 

       int answerAttempt = 3; 
       do { 
        System.out.print(" 3 * 8 = "); 
        answer = input.nextInt(); 
        input.nextLine(); 
        answerAttempt--; 

        if (answer != 24 && answerAttempt >0) 
         System.out.printf(
           "Invalid! Try Again! %d attempt(s) left!\n ", 
           answerAttempt); 

       } while (answerAttempt >0 && answerAttempt < 3 && answer != 24); 

       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); 
       } 

      } 

      mainMenuAttempt--; 

     } while (mainMenuAttempt >0 && mainMenuAttempt < 3 && !wordOfTheDay.equals("tired") && answer!=24); 
     System.exit(0); 
    } 
} 
2

試着把整個事情分解成非常小的函數,分別做一件特定的事情。例如,您可能有一個函數,用於檢查單詞是否正確,如果是則返回0,否則返回0。

private int checkWord(String correctWord, String wordToCheck, int attempts) 
{ 
    if(wordToCheck.equals(correctWord)) 
    { 
     return 0; 
    } 
    return attempts; 
} 

然後,您可以創建一個函數,它接受用戶輸入,並調用該函數來檢查所述輸入。然後它可以輸出一個依賴於前一個函數返回碼的錯誤信息。該函數然後會返回一個代碼來告訴上面的函數情況。

public int checkWordVerbose(String question, String correctWord, int attempts) 
{ 
    if(attempts <= 3) 
    { 
     Scanner scan = new Scanner(System.in); 
     System.out.print(question); 
     String input = scan.nextLine(); 
     int failureCode = checkWord(correctWord, input, attempts); 

     if(failureCode == 0) 
     { 
      return 0; 
     } 
     else 
     { 
      System.out.println("Invalid! Attempts left: " + (3 - failureCode)); 
      return 1; 
     } 
    } 
    else 
    { 
     System.out.println("Sorry! You have no more attempts left!\n"); 
     return 2; 
    } 
} 

最後,你可以把它簡單地包含了循環的功能,並保持邏輯保持這個循環:

public int checkWord(String correctWord) 
{ 
    int failureCode = 1; 
    int attempts = 1; 
    do 
    { 
     failureCode = checkWordVerbose("Enter the word of the day: ", correctWord, attempts); 
     attempts++; 
    } while(failureCode == 1); 
    return failureCode; 
} 

然後在主,所有你需要做的是檢查是否返回checkWord(String correctWord) 0.重複同樣的事情爲您的其他檢查(或者你甚至可能能夠使用一些相同的功能),執行另一個,如果在第一個,如:

if(checkWord("tired") == 0) 
{ 
    if(checkMath("24") == 0) 
    { 
     // Success! 
    } 
}