![](/img/trans.png)
[英]How to make a particular string input the condition for while loop and it checks everytime an input is entered
[英]How to set up a loop condition that ends when a particular string input is entered
這是我的問題的輸入部分。 編寫一個程序,讓用戶不斷輸入以下內容:
* 3 個不同實例的溫度
3 個輸入的溫度單位(攝氏度、華氏度、開爾文)
用於轉換的溫度單位(攝氏度、華氏度、開爾文)
如果不滿足這些條件或 output 已打印,則它將返回詢問
用戶輸入新值。
如果在輸入階段的任何時候用戶輸入單詞“退出”然后結束程序。 *
我能夠解決問題中的所有問題,但我跳過了上面的最后一個條件,因為我不知道如何設置它。 現在這是我程序的輸入階段:
Scanner sc = new Scanner(System.in);
System.out.println("Please give three numbers");
int num1 = sc.nextInt();
int num2 = sc.nextInt();
int num3 = sc.nextInt();
System.out.println("Choose the temperature unit of the three numbers.");
System.out.println("Enter 1 for Celsius, 2 for Fahrenheit and 3 for Kelvin");
int tempUnit = sc.nextInt();
switch (tempUnit) {
case (1) -> System.out.println("Celsius was chosen.");
case (2) -> System.out.println("Fahrenheit was chosen.");
case (3) -> System.out.println("Kelvin was chosen.");
}
System.out.println("Choose the temperature unit you want to convert it into.");
System.out.println("Enter 1 for Celsius, 2 for Fahrenheit and 3 for Kelvin");
int chosenTemp = sc.nextInt();
switch (chosenTemp) {
case (1) -> System.out.println("Celsius was chosen.");
case (2) -> System.out.println("Fahrenheit was chosen.");
case (3) -> System.out.println("Kelvin was chosen.");
}
只要在任意點輸入字符串“Quit”,循環結束程序就結束了。
頂部的 while 循環將完成檢查輸入“退出”是否已輸入的條件
一切,但我大部分時間都在失敗,因為如果使用不同的數據類型,我會收到錯誤
輸入的內容比被問到的要多。
有人告訴我,我的提示沒有設置為接收我的 while 循環的“退出”。 任何人都可以
給我一個例子或教我如何設置它?
首先,我們創建一些輔助方法來讀取用戶輸入:
public static int readNumber(){
Scanner sc = new Scanner(System.in);
String string = sc.nextLine(); //Read anything that user typed
//Terminate program if "quit" was typed
if(string.equalsIgnoreCase("quit")){
System.exit(0);
}
try {
return Integer.parseInt(string);
} catch (Exception e){
return readNumber(); //If user typed gibberish - retry
}
}
public static int readNumber(int lowerBound, int upperBound){
int number = readNumber();
if(number < lowerBound || number > upperBound){
return readNumber(lowerBound, upperBound);
}
return number;
}
之后更改主程序:
public static void main(String[] args) {
//Run program indefinitely
while(true) {
System.out.println("Please give three numbers");
int num1 = readNumber();
int num2 = readNumber();
int num3 = readNumber();
System.out.println("Choose the temperature unit of the three numbers.");
System.out.println("Enter 1 for Celsius, 2 for Fahrenheit and 3 for Kelvin");
int tempUnit = readNumber(1, 3);
switch (tempUnit) {
case 1: System.out.println("Celsius was chosen.");
case 2: System.out.println("Fahrenheit was chosen.");
case 3: System.out.println("Kelvin was chosen.");
}
System.out.println("Choose the temperature unit you want to convert it into.");
System.out.println("Enter 1 for Celsius, 2 for Fahrenheit and 3 for Kelvin");
int chosenTemp = readNumber(1, 3);
switch (chosenTemp) {
case 1: System.out.println("Celsius was chosen.");
case 2: System.out.println("Fahrenheit was chosen.");
case 3: System.out.println("Kelvin was chosen.");
}
}
}
將獲取用戶輸入的邏輯移到單獨的方法中。 即使在小型程序中,這通常也是一個好主意,因為它允許您一次專注於一件事。
private int readInt();
靜態定義 Scanner 或將其作為輸入提供。 為簡單起見,我將說我們將其作為輸入提供,因此我們將其添加為參數。
private int readInt(Scanner scanner);
該方法將從用戶那里讀取一行輸入,如果匹配“退出”則終止程序,然后解析並返回 integer 值。 它將報告錯誤輸入並允許用戶重試,直到他們退出或輸入有效的 int 值。
private int readInt(Scanner scanner) {
while (true) {
String line = scanner.readLine();
if ("quit".equalsIgnoreCase(line)) {
System.exit(0);
}
try {
return Integer.parse(line);
} catch (Exception e) {
System.out.println("That does not appear to be an integer - try again");
}
}
}
用法看起來像
Scanner scanner = new Scanner(System.in);
... stuff ...
int value = readInt(scanner);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.