2016-02-22 163 views
0

我想要得到這個一遍又一遍地問問題,而用戶輸入一個'Y'。並停止並返回Event.displayFinalResults();當用戶輸入一個'N'雖然循環不工作在Java

我現在正在一個連續的循環。我錯過了一些東西或者我的佈局錯了。任何幫助都會很棒。

  System.out.println("Is there anymore amounts you want to add?(Y/N): "); 
      Scanner keyboard = new Scanner(System.in); 
      char choice = keyboard.next().charAt(0); 
      choice = Character.toUpperCase(choice); 
      if(choice == 'Y') 
      { 
       do 
       { 
        System.out.println("Is there anymore amounts you want to add?(Y/N): "); 
        choice = keyboard.next().charAt(0); 
        choice = Character.toUpperCase(choice); 

        readValidateAmountType(); 
        readValidateAmount(); 
       } 
       while(choice =='Y'); 

      } 
      else 
      { 
       Event.displayFinalResults(); 
      } 

再次感謝。

+0

只是要清楚,問題是在你先輸入Y之後,然後輸入N,它會留在while循環中嗎? – Michael

+1

它比較字符,而不是字符串,所以我不認爲這是問題。 – Michael

+0

我是新的。所以我知道我有一個連續的循環,因爲一個小問題。我一直在努力嘗試不同的方法幾個小時,現在我處於障礙之中。 – Luke8h

回答

3

您的程序詢問是/否問題,那麼如果您回答是,它會在詢問金額值之前再次詢問相同的問題開始循環。

您可能需要在之前詢問金額值,然後再次詢問是/否問題。

如果用戶回答否,循環將退出(請求多​​一個金額值後),但Event.displayFinalResults()將不會執行。刪除else子句,因此Event.displayFinalResults()是否輸入if聲明與否。

System.out.println("Is there anymore amounts you want to add?(Y/N): "); 
Scanner keyboard = new Scanner(System.in); 
char choice = keyboard.next().charAt(0); 
choice = Character.toUpperCase(choice); 
if (choice == 'Y') 
{ 
    do 
    { 
     readValidateAmountType(); 
     readValidateAmount(); 

     System.out.println("Is there anymore amounts you want to add?(Y/N): "); 
     choice = keyboard.next().charAt(0); 
     choice = Character.toUpperCase(choice); 
    } 
    while(choice =='Y'); 
} 
Event.displayFinalResults(); 
+0

謝謝@Andreas! – Luke8h

0

我發現的一個問題是,您的while循環位於if語句中。一旦你退出while循環,它不會運行else塊內的代碼,因爲if條件已經成立。

因此,請嘗試刪除else塊。這將確保while循環退出時調用Event.displayFinalResults方法。

 System.out.println("Is there anymore amounts you want to add?(Y/N): "); 
     Scanner keyboard = new Scanner(System.in); 
     char choice = keyboard.next().charAt(0); 
     choice = Character.toUpperCase(choice); 

if(choice == 'Y') 
     { 
      do 
      { 
       System.out.println("Is there anymore amounts you want to add?(Y/N): "); 
       choice = keyboard.next().charAt(0); 
       choice = Character.toUpperCase(choice); 

       readValidateAmountType(); 
       readValidateAmount(); 
      } 
      while(choice =='Y'); 

     } 

     Event.displayFinalResults(); 
2

如下你可以做到這一點使用break;

do { 
    System.out.println("Is there anymore amounts you want to add?(Y/N): "); 
    choice = keyboard.next().charAt(0); 
    choice = Character.toUpperCase(choice); 
    if (choice !='Y') { 
     break; 
    } 

    readValidateAmountType(); 
    readValidateAmount(); 
} while(true); 

Event.displayFinalResults(); 
-1

嘗試以下操作進行:

public void answering(char answer){ 
     if(answer == 'Y' || answer == 'y'){ 
      System.out.println("Answering"); 
     } else if(answer == 'N' || answer == 'n'){ 
      System.out.println("Not answering"); 
    } 

而是循環的,調用此方法時,你要問...

0

清除代碼和緊湊:

Scanner keyboard = new Scanner(System.in); 
char choice; 
do { 
    System.out.println("Is there anymore amounts you want to add?(Y/N): "); 
    choice = keyboard.next().charAt(0); 
    choice = Character.toUpperCase(choice); 
    if(choice == 'Y') { 
     readValidateAmountType(); 
     readValidateAmount(); 
    } 
} while(choice == 'Y');  
Event.displayFinalResults();