[英]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.