[英]multiple variable for if else statement
我寫了一些代碼來檢查用戶是否輸入了1到5之間的數字,現在我還希望我的代碼允許用戶輸入字母A,S,D或M.
有沒有辦法將代碼組合在一起,我可以識別用戶是否輸入了1-5或A,S,D,M?
如何編輯下面的代碼,以便用戶可以輸入整數或字符? 我是否必須在循環下面編寫一段代碼,以便識別用戶沒有輸入1-5但是確實輸入A,S,D或M,就像在循環中一樣? 或者它是一個單獨的循環。 我感到很困惑!
import java.util.InputMismatchException;
import java.util.Scanner;
public class Selection {
Scanner readInput = new Scanner(System.in);
int selectionOne() {
int inputInt;
do { //do loop will continue to run until user enters correct response
System.out.print("Please enter a number between 1 and 5, A for Addition, S for subtraction, M for multiplication, or D for division: ");
try {
inputInt = readInput.nextInt(); //user will enter a response
if (inputInt >= 1 && inputInt <=5) {
System.out.print("Thank you");
break; //user entered a number between 1 and 5
} else {
System.out.println("Sorry, you have not entered the correct number, please try again.");
}
continue;
}
catch (final InputMismatchException e) {
System.out.println("You have entered an invalid choice. Try again.");
readInput.nextLine(); // discard non-int input
continue; // loop will continue until correct answer is found
}
} while (true);
return inputInt;
}
}
我建議不要使用int
輸入,只需使用String
輸入並在需要時將其轉換為整數。 您可以使用Integer.parseInt(String)
將String
轉換為int
。
因此,當您檢查輸入是否有效時,您需要檢查輸入是否等於"A"
, "S"
, "M"
或"D"
,或者轉換為int
時的1-5中的任何值。
所以要檢查它是否是其中一個字符,你可以這樣做:
if (input.equals("A") || input.equals("S") || input.equals("M") || input.equals("D"))
然后測試它是否是值為1到5的int
,你可以這樣做:
if (Integer.parseInt(input) >= 1 && Integer.parseInt(input) <= 5)
只需將輸入解析為int
,然后像您已經完成的那樣檢查范圍。
此方法的返回類型現在是String
,而不是int
。 如果由於某種原因需要它是一個int
,你可以將值解析為int
然后返回它。 但我只是把它作為一個String
返回。
我改變的最后一件事是catch
塊。 現在,而不是InputMismatchException
(因為它們現在可以輸入String
,我將其更改為NumberFormatException
,如果嘗試無法轉換為int
的String
,則會發生這種情況。例如, Integer.parseInt("hello")
將拋出NumberFomatException
因為"hello"
不能表示為整數。但是, Integer.parseInt("1")
會很好,並返回1
。
請注意,您應首先測試String
等效性,以便在有機會測試所需的所有條件之前不要進入block
。
該方法如下所示:
String selectionOne() {
String input;
do { //do loop will continue to run until user enters correct response
System.out.print("Please enter a number between 1 and 5, A for Addition, S for subtraction, M for multiplication, or D for division: ");
try {
input = readInput.nextLine(); //user will enter a response
if (input.equals("A") || input.equals("S") || input.equals("M") || input.equals("D")) {
System.out.println("Thank you");
break; //user entered a character of A, S, M, or D
} else if (Integer.parseInt(input) >= 1 && Integer.parseInt(input) <= 5) {
System.out.println("Thank you");
break; //user entered a number between 1 and 5
} else {
System.out.println("Sorry, you have not entered the correct number, please try again.");
}
continue;
}
catch (final NumberFormatException e) {
System.out.println("You have entered an invalid choice. Try again.");
continue; // loop will continue until correct answer is found
}
} while (true);
return input;
}
正如@MarsAtomic所提到的,首先應該將輸入更改為String
而不是int
這樣您就可以輕松處理字符和數字。
更改:
int inputInt;
至:
String input;
然后改變:
inputInt = readInput.nextInt();
至:
input = readInput.next();
容納讀取String
而不是int
。
現在你達到2個主要案例(和2個子句):
1) input is a single character
a) input is a single digit from 1-5
b) input is a single character from the set ('A', 'S', 'D', 'M')
2) input is an error value
此外,由於您沒有調用Scanner.nextInt
,因此您不需要使用try/catch
語句,並且可以在else
塊中打印您的錯誤。
此外,您應該讓您的方法返回char
或String
而不是int
這樣您就可以返回1-5
或A,S,D,M
。 我假設你想要歸還一個char
。 如果要返回String
,則可以在下面的代碼中return input
而不是return val
。
注意: 下面的代碼可以簡化和縮短,我只是添加了變量和注釋,試圖使每個步驟清楚地顯示正在讀取或轉換的內容。 您可以通過@ mikeyaworski的答案來了解更簡潔的方法。
以下是您的代碼的外觀:
char selectionOne() {
String input;
do {
input = readInput.next();
// check if input is a single character
if(input.length() == 1) {
char val = input.charAt(0);
// check if input is a single digit from 1-5
if(Character.isDigit(val)) {
int digit = Integer.parseInt(input);
if (digit >= 1 && digit <=5) {
System.out.print("Thank you");
return val; // no need to break, can return the correct digit right here
} else {
System.out.println("Sorry, you have not entered the correct number, please try again.");
}
} else {
// check if input is in our valid set of characters
if(val == 'A' || val == 'S' || val == 'M' || val == 'D') {
System.out.print("Thank you");
return val; // return the correct character
} else {
System.out.println("Sorry, you have not entered the correct character, please try again.");
}
}
} else {
System.out.println("Sorry, you have not entered the correct input format, please try again.");
}
} while(true);
}
如果您的輸入可以是字符和字母,為什么不更改為查找char或String? 然后,您可以毫無困難地查找“1”或“A”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.