检查值是否在 Java 中的两个数字之间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28752782/
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
Check if value is between two numbers in Java
提问by Crimson
I am in a programming class and we are making a simple program. In this program the user will put in five assignments, the points they got on that assignment and the total points available. I have all of the basic things except for a grading scale. I am trying to make it so when their percent is between 92 and 100 percent it will say you have an A in this class. This is what I have so far:
我正在上编程课,我们正在制作一个简单的程序。在此程序中,用户将进行五项作业,他们在该作业中获得的分数以及可用的总分数。除了评分等级之外,我拥有所有基本的东西。我试图做到这一点,当他们的百分比在 92% 到 100% 之间时,它会说你在这门课上得了 A。这是我到目前为止:
if ( pC >= 92, pC <= 100 ) {
System.out.print("\n You have an B");
}
So far that has not worked and I am having a lot of trouble.
到目前为止,这还没有奏效,我遇到了很多麻烦。
回答by Sam Hanley
What you're trying to do is check if a number is >= 92 AND <= 100. That isn't done with a comma, like you have:
你要做的是检查一个数字是否 >= 92 AND <= 100。这不是用逗号完成的,就像你有:
if ( pC >= 92, pC <= 100 ) {
System.out.print("\n You have an B");
}
Rather, the AND operator is &&
(and you said you want this range to be an A, rather than a B), which means your code would look like this:
相反,AND 运算符是&&
(并且您说您希望此范围是 A,而不是 B),这意味着您的代码将如下所示:
if ( pC >= 92 && pC <= 100 ) {
System.out.print("\n You have an A");
}
回答by Laerte
I think what you are looking for is this:
我想你要找的是这个:
If the point is between 92% and 100%:
如果点在 92% 和 100% 之间:
if ( pC >= 92 && pC <= 100 ) {
System.out.print("\n You have an A");
}
If the point is out that range:
如果该点超出该范围:
if ( pC < 92 ) {
System.out.print("\n You have an B");
}
Or, to the complete statement, you will want to make:
或者,对于完整的陈述,您需要做出以下声明:
if ( pC >= 92 && pC <= 100 ) {
System.out.print("\n You have an A");
}
else if ( pC < 92 ){
System.out.print("\n You have an B");
}
else {
System.out.print("\n You have a point out of the curve");
}