2016-10-01 16 views
1

我正在嘗試爲我的練習編寫一個程序流程圖,其中我遇到以下情況對我來說需要用戶輸入字符串值作爲輸入「是「或‘Y’和‘否’或‘N’,並根據其輸入端的應用程序將終止或從某一點重新啓動到現在我在我的腦海裏有這個作爲一個例子如何使用字符串變量作爲循環目的的條件

public class ApplicationName { 

    public static void main(String args[]) { 
     String restartOperation; 
     do { 
      restartOperation = Confirm_Before_Exit(); 
     } while (!restartOperation.equals("No")); 

     //Rest of code 
    } 

    public static void Some_Operation() { 
     //Executed when called before closing application 
    } 

    public static String Confirm_Before_Exit() { 
     Scanner inputData = new Scanner(System.in); 
     String answer; 
     System.out.println("Do you want to perform another operation ?" + " " + "Y" + " " + "N"); 
     answer = inputData.nextLine(); 
     switch (answer) { 
      case "Y": 
      case "Yes": 
       Some_Operation(); 
       break; 
      default: 
       System.out.println("Good Bye !"); 

     } 
     return answer; 
    } 
} 

這工作,直到用戶沒有給出輸入爲「否」,但顯然如果輸入「N」或可能小「n」或甚至「否」不會工作,但對於時間我只是試圖將「否」和「N」作爲輸入值。

回答

1

你做出改變,而以下幾點:

do { 
     restartOperation = Confirm_Before_Exit(); 
} while (!restartOperation.equalsIgnoreCase("No") && !restartOperation.equalsIgnoreCase("n")); 
+0

它再次陷入無限循環,並且不斷詢問即使輸入「n」或「no」 –

+0

@MehulChachada更新了答案。 '||'需要用'&&'代替。試試看。它現在的作品 –

+0

它的作品謝謝! 〜 –

1

我會做這樣的事情。你把你的答案寫成小寫,以便考慮像YE,NO等情況。然後你指定n和no。默認值應該是全部。

answer = inputData.nextLine().toLowerCase(); 
    switch (answer) { 
     case "y": 
     case "yes": 
      Some_Operation(); 
      break; 
     case "n": 
     case "no": 
      System.out.println("Good Bye !"); 
      break; 
     default: 
      System.out.println("Not a valid reponse"); 

    } 
+0

這是一個錯誤的答案,不會工作,因爲在'do-while'循環中,他正在檢查'No'。這是需要改變的。 –

+0

是的,它不工作 –

+0

@Pankaj Singhal良好的捕獲。我錯過了那部分。 – mfcastro

0

您可能需要使用正則表達式文本匹配(它包含在默認的Java JDK,而不是外部庫)。它非常強大。

你定義一個針正則表達式的語法來搜索,例如這樣的:

^no?$ 

它與隨後on並最終(?)匹配啓動文本(^)(但它確實不需要在那裏),然後文本結束($)。還有一個不區分大小寫匹配的標誌/i。您可以嘗試Regexregex101.com。試試這個:regex101.com/r/edrehj

好吧,如何使用這個東西在Java?相當簡單:在Wikipedia#RegexJava-API#PatternJava-API#Matcher

String input = ... 
Pattern pattern = Pattern.compile("^no?$", Pattern.CASE_INSENSITIVE); 
Matcher matcher = pattern.matcher(input); 
if (matcher.find()) { 
    // User want's to cancel 
    ... 
} else { 
    // User want's to continue 
    ... 
} 

更多信息。

你也可以把這樣的一個自己的方法裏面,使得使用更加方便,人性化閱讀:

private boolean doesUserWantToCancel(final String textInput) { 
    Pattern pattern = Pattern.compile("^no?$", Pattern.CASE_INSENSITIVE); 
    Matcher matcher = pattern.matcher(textInput); 
    return matcher.find(); 
} 

然後,您只需調用方法您輸入:

... 
} while (!doesUserWantToCancel(restartOperation)); 
... 
+0

有沒有其他方式不使用Java API,只使用java? –

+0

正則表達式是本地Java的一部分。它是純粹的Java,你可以在沒有任何外部庫或東西的情況下使用它。把上面的代碼放到你的程序中 - 它會起作用。 – Zabuza

0

如果我正確理解你的問題,你想在重新啓動一個操作時運行Confirm_Before_Exit()

這是我會做的。您應該擁有boolean restartOperation而不是string restartOperation。這樣你或者處理真或假。

Confirm_Before_Exit()函數中,我仍然會將.toLowerCase()添加到inputData中,以考慮奇怪的情況。我添加了兩個布爾值answerinValidAnswer

如果您想要執行其他操作,您希望您的答案爲真。如果它是假的,它將退出。

如果用戶錯誤輸入,inValideAnswer將被設置爲true,並且他們會收到一個錯誤,提示無效的答案並將被重新提示。

public class ApplicationName { 

    public static void main(String args[]) { 
     boolean restartOperation; 
     do { 
      restartOperation = Confirm_Before_Exit(); 
     } while (restartOperation); 

     //Rest of code 
    } 

    public static void Some_Operation() { 
     //Executed when called before closing application 
    } 

    public static boolean Confirm_Before_Exit() { 
     Scanner inputData = new Scanner(System.in); 
     boolean answer; 
     boolean validAnswer; 

    while(inValideAnswer){ 
     System.out.println("Do you want to perform another operation ?" + " " +  "Y" + " " + "N"); 
     string userInput = inputData.nextLine().toLowerCase(); 

     switch (userInput) { 
     case "y": 
     case "yes": 
      Some_Operation(); 
      answer = true; 
      inValideAnswer = false; 
      break; 
     case "n": 
     case "no": 
      answer = false; 
      inValideAnswer = false; 

      System.out.println("Good Bye !"); 
      break; 
     default: 
      System.out.println("Not a valid reponse"); 
      inValideAnswer = true; 

     } 

    } 

    return answer; 
    } 



} 
相關問題