2014-09-29 46 views
-1

我在Do Else While語句後需要兩個結果,現在用戶可以輸入數據,並且它將存儲在一個字符串中,如果他們想要添加其他任何他們鍵入的'y',如果'n'它會結束編程並告訴他們他們輸入了什麼。如果他們輸入既不是那些和輸入「d」的,例如它停止語句運行,帶我一直到else語句如何在Do語句終止後得到兩個結果?

else語句我想兩個結果,無論是「你已經添加了以下」和「錯誤,你輸入什麼不對」

這裏是DO否則雖然聲明:

do { 
     System.out.println("Current list is " + list); 
     System.out.println("Add more? (y/n)"); 
     if (input.next().startsWith("y")) { 
      System.out.println("Enter : "); 
      list.add(input.next()); 
     } else { 
      System.out.println("You have added the following:"); 
      System.out.println("Error, you inputted something wrong"); 
      break; 

     } 
    } while (true); 

什麼我寫來獲得這取決於用戶做了兩個結果? (說'n'或寫錯了什麼)。

+0

只是放了另一個'if'(或更好的'else if')。 – qqilihq 2014-09-29 18:45:33

+0

你會認爲「錯誤」是什麼?一個數字而不是一個字符串? – MarGar 2014-09-29 18:47:13

+0

第一次調用'input.next()'讀取一個單詞,第二次調用讀取第二個單詞,所以你會讀'y 1 y 2 y 3 y 4 y 5',但是你不能說'n '因爲這將是一個錯誤。 :P – 2014-09-29 18:47:25

回答

0

試試這個:

do { 
    System.out.println("Current list is " + list); 
    System.out.println("Add more? (y/n)"); 
    if (input.next().startsWith("y")) { 
     System.out.println("Enter : "); 
     list.add(input.next()); 
    } else if (input.next().startsWith("n")) { 
     System.out.println("You have added the following:"); 
     break; 
    } else { 
     System.out.println("Error, you inputted something wrong"); 
    } 
} while (true); 
+0

謝謝,這完美! – AAP 2014-09-29 19:36:48

0

在else中引入另一個條件。最好使用其他如:

do { 
    System.out.println("Current list is " + list); 
    System.out.println("Add more? (y/n)"); 
    String userInput = input.next(); 
    if (userInput.startsWith("y")) { 
     System.out.println("Enter : "); 
     list.add(input.next()); 
    } elseif (userInput.startsWith("n")) { 
     // user wants to stop 
     System.out.println("You have added the following:"); 
     break; 
    } else { 
     System.out.println("Error, you inputted something wrong"); 
     break; 
    } 
} while (true); 
+0

謝謝,這工作!我現在唯一的問題是它在註冊之前需要兩個輸入。這是輸出: 添加更多? ñ ň 您已經添加以下(Y/N): – AAP 2014-09-29 19:33:41

1

只需要添加另外if-else:

do { 
    System.out.println("Current list is " + list); 
    System.out.println("Add more? (y/n)"); 
    if (input.next().startsWith("y")) 
    { 
    System.out.println("Enter : "); 
    list.add(input.next()); 
    } 
    else 
    { 
    if(//valid input condition) 
     System.out.println("You have added the following:"); 
     else 
     System.out.println("Error, you inputted something wrong"); 
    break; 
    } 
} while (true); 
0

嘗試使用 「IF」 語句中else語句

if (input.next().startsWith("y")) { 
     System.out.println("Enter : "); 
     list.add(input.next()); 
    } else { 
     if (input.next().startsWith("n")) { 
      // Your code for "n" 
     }else{ 
      //else here. 
      System.out.println("You have added the following:"); 
      System.out.println("Error, you inputted something wrong"); 
      break; 
     } 

    } 
0

我認爲你的程序的一些邏輯和流程稍微偏離。我會改變這一點。

代碼:

boolean keepGoing = true; // can use a boolean to change the while loop condition to false. 

    while (keepGoing) { 

     System.out.println("Enter : "); 
     list.add(input.next());    

     System.out.println("Current list is " + list); 
     System.out.println("Add more? (y/n)"); 

     if (input.next().startsWith("y")) { // 'if' to check if 'y', then execute this code.   

      keepGoing = true; // don't really need this, but it's here as example 

     } else if (input.next().startsWith("n")){ // 'else if' to check if 'n'. 

      System.out.println("You have added the following: " + list);    
      keepGoing = false; //change to false to stop the loop 

     } else { // and lastly a single 'else' if the input was invalid based on 2 previous conditions. 

     System.out.println("Error, you inputted something wrong"); // if for some reason the input isn't accepted this will show. 

     } 

    } 

它遵循一個更合乎邏輯的流動,更容易理解。一個簡單的while循環對其他人來說更容易理解,他們可以在進入循環體之前評估條件。

你也不需要布爾值,可以簡單地在else if部分while循環和break;使用true,但與while循環,休息可以儘可能在別人需要看你的代碼中創建的困惑一旦你開始寫更大的程序。