如何在 if/else 语句中包含一系列数字 Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32171744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to have a range of numbers in an if/else statement Java
提问by Voltar
I'm learning Java through a series of explanations and exercises, and one of them was to create a program that would display a number grade (0-5) in accordance to a number of points (0–29, 30–34, 35–39, 40–44, 45–49, 50–60).
我正在通过一系列的解释和练习来学习 Java,其中一个是创建一个程序,该程序将根据多个点(0-29、30-34、35)显示一个数字等级(0-5) –39、40–44、45–49、50–60)。
System.out.println("Type the points [0-60]: ");
double points = reader.nextDouble();
reader.nextLine();
if (points < 29) {
System.out.println("Grade: FAILED.");
} else if (points <= 34) {
System.out.println("Grade: 1.");
} else if (points <= 39) {
System.out.println("Grade: 2.");
} else if (points <= 44) {
System.out.println("Grade: 3.");
} else if (points <= 49) {
System.out.println("Grade: 4.");
} else if (points >= 50) {
System.out.println("Grade: 5.");
}
The program works in that it'll give the correct grade because of the overlap in commands, but is there any way to create a range of numbers or strings that could meet the condition of the if/else statement? For example, if the number entered is between 40-44 and so on. Detailed answer would be appreciated since I'm new.
该程序的工作原理是,由于命令中的重叠,它会给出正确的等级,但是有没有办法创建一系列可以满足 if/else 语句条件的数字或字符串?例如,如果输入的数字在 40-44 之间等等。由于我是新手,因此将不胜感激。
采纳答案by itwasntme
If you want to test if number is between some values use logical operator AND for if statements like:
如果要测试数字是否在某些值之间,请使用逻辑运算符 AND 用于 if 语句,例如:
if(points>=40 && points <=45)
To get more clarity I would suggest you to make a control inversion like this:
为了更清楚,我建议您进行如下控制反转:
int grade;
if(points >49){
grade=5;
}else if(points >44){
grade=4;
}else if(points >39){
grade=3;
}else if(points >34){
grade=2;
}else if(points >29){
grade=1;
}else{
grade=0;
}
System.out.println("Gr: "+grade); //grade=0 = not passed
回答by Stack Player
I think you might be looking for
我想你可能正在寻找
if( points<=44 && points>=40 )
回答by joeya17
If you want to check if a number is between two values, you would use an and in your if statement:
如果要检查数字是否介于两个值之间,可以在 if 语句中使用 and :
if (points >= 40 && points <= 44) {
System.out.println("Grade: 3.");
}
回答by dasblinkenlight
Divide the number by five, and make an array of twelve strings with the textual representation of the grade:
将数字除以 5,并使用等级的文本表示制作一个包含 12 个字符串的数组:
private static final String GRADE_FAILED = "FAILED";
private static final String GRADE_1 = "1";
private static final String GRADE_2 = "2";
private static final String GRADE_3 = "3";
private static final String GRADE_4 = "4";
private static final String GRADE_5 = "5";
private static String GradeStr[] = new {
GRADE_FAILED // 0..4
, GRADE_FAILED // 5..9
, GRADE_FAILED // 10..14
, GRADE_FAILED // 15..19
, GRADE_FAILED // 20..24
, GRADE_FAILED // 25..29
, GRADE_1 // 30..34
, GRADE_2 // 35..39
, GRADE_3 // 40..44
, GRADE_4 // 45..49
, GRADE_5 // 50..54
, GRADE_5 // 55..59
, GRADE_5 // 60..64 // Only sixty matters
};
System.out.println("Grade:"+GradeStr[points/5]+".");
If you need more granularity, make a bigger array, divide by a smaller number or skip the division altogether, and set the constants in the positions that correspond to the grades that need to be printed. This lets you avoid the conditionals altogether.
如果需要更大的粒度,可以做一个更大的数组,除以较小的数字或完全跳过除法,并将常量设置在与需要打印的等级对应的位置。这使您可以完全避免条件。
Important disclaimer:this approach works best when the number of options is small - up to a hundred or so. When your problem allows for this approach, it is the fastest approach by far. In many cases, it is also the easiest one to read.
Important disclaimer:this approach works best when the number of options is small - up to a hundred or so. 当您的问题允许使用这种方法时,它是迄今为止最快的方法。在许多情况下,它也是最容易阅读的。
回答by user3437460
If you want to check for a range within an if statement, you can do this:
如果要检查 if 语句中的范围,可以执行以下操作:
if(points >= 0 && points <= 29)
This is by using &&
(and) operator by combining conditions together.
这是通过使用&&
(and) 运算符将条件组合在一起。
But you don't really need to check for a range in any of the if-statements. Doing what you currently have will be suffice.
但是您实际上并不需要检查任何 if 语句中的范围。做你目前拥有的就足够了。
回答by Andreas
You can use a NavigableMap
, most commonly a TreeMap
. The method used below is floorEntry
. Quoting Javadoc:
您可以使用 a NavigableMap
,最常见的是 a TreeMap
。下面使用的方法是floorEntry
。引用Javadoc:
Returns a key-value mapping associated with the greatest key less than or equal to the given key, or
null
if there is no such key.
返回与小于或等于给定键的最大键相关联的键值映射,或者
null
如果没有这样的键。
Note:Your code was missing an =
sign on the 29 boundary, and the points
value should be an integer.
注意:您的代码=
在 29 边界上缺少一个符号,该points
值应为整数。
Changedto use a grade (0-5), instead of the string used in question.
更改为使用等级 (0-5),而不是使用的字符串。
// Grade boundary is lower-inclusive (grade is 0-60)
TreeMap<Integer, Integer> gradeMap = new TreeMap<>();
gradeMap.put( 0, 0); // 0–29
gradeMap.put(30, 1); // 30–34
gradeMap.put(35, 2); // 35–39
gradeMap.put(40, 3); // 40–44
gradeMap.put(45, 4); // 45–49
gradeMap.put(50, 5); // 50+
System.out.println("Type the points [0-60]: ");
int points = reader.nextInt();
reader.nextLine();
int grade = gradeMap.floorEntry(points).getValue();
System.out.println("Grade: " + grade);