Java 运算符 > 未定义参数类型布尔型、双精度型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22305632/
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
The operator > is undefined for the argument type(s) boolean, double
提问by user3402545
I am a new programmer, so sorry if this is really basic. I have looked around for this website for an answer, I could find very similar questions, but none was what I needed.
我是一名新程序员,如果这真的很基础,那么抱歉。我在这个网站上四处寻找答案,我可以找到非常相似的问题,但没有一个是我需要的。
import java.util.Scanner;
public class sortThreeIntegers
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);
Scanner input3 = new Scanner(System.in);
System.out.println("Enter the first number: ");
System.out.println("Enter the second number: ");
System.out.println("Enter the third number: ");
double firstNumber = input.nextDouble();
double secondNumber = input2.nextDouble();
double thirdNumber = input3.nextDouble();
if (firstNumber > secondNumber > thirdNumber == true)
{
System.out.println(firstNumber + ", " + secondNumber + ", " + thirdNumber);
}
else if (firstNumber > thirdNumber > secondNumber == true)
{
System.out.println(firstNumber + ", " + thirdNumber + ", " + secondNumber);
}
}
}
Note: This is in the middle of me writing the code, I am not finished with this.
注意:这是在我编写代码的过程中,我还没有完成。
Why am I getting the error "The operator > is undefined for the argument type(s) boolean, double"? Again, sorry if this is really simple.
为什么我收到错误“运算符 > 未定义参数类型布尔型,双精度”?再次,对不起,如果这真的很简单。
采纳答案by arshajii
if (firstNumber > secondNumber > thirdNumber == true)
This is invalid; you want:
这是无效的;你要:
if (firstNumber > secondNumber && secondNumber > thirdNumber)
The same goes for your else if
condition.
你的else if
情况也是如此。
You cannot chain comparisons in Java like you can in, say, Python. Also, there is never a need for an == true
in boolean expressions. After all, x == true
will always be x
.
您不能像在 Python 中那样在 Java 中进行链式比较。此外,永远不需要== true
in 布尔表达式。毕竟x == true
永远都是x
。
The error you received makes sense, since your expression is being evaluated as:
您收到的错误是有道理的,因为您的表达式被评估为:
(firstNumber > secondNumber) > thirdNumber
Now (firstNumber > secondNumber)
is a boolean, which cannot be compared to a double (thirdNumber
).
Now(firstNumber > secondNumber)
是一个布尔值,无法与双精度 ( thirdNumber
)进行比较。
Finally, it looks like you're just trying to output the numbers in sorted order. Why not just sort them regularly, then output the result? Also: don't use three different scanners! Just use the same one three times.
最后,看起来您只是想按排序顺序输出数字。为什么不定期对它们进行排序,然后输出结果?另外:不要使用三种不同的扫描仪!只需使用相同的三遍。
This is what I mean by sorting the numbers:
这就是我对数字进行排序的意思:
double numbers[] = {firstNumber, secondNumber, thirdNumber};
Arrays.sort(numbers);
System.out.println(numbers[2] + ", " + numbers[1] + ", " + numbers[0]);
回答by AntonH
You can't have this : if (firstNumber > secondNumber > thirdNumber == true)
.
你不能有这个:if (firstNumber > secondNumber > thirdNumber == true)
。
You need to break it down to:
您需要将其分解为:
if (firstNumber > secondNumber && secondNumber > thirdNumber) {
回答by rgettman
You cannot combine operators such as >
as we would do as a shortcut in expressing a mathematical inequality. You must list out such expression explicitly and join the expressions with the &&
and operator.
您不能>
像我们那样组合运算符作为表达数学不等式的捷径。您必须明确列出此类表达式,并使用&&
and 运算符连接这些表达式。
if (firstNumber > secondNumber && secondNumber > thirdNumber) {
Also, I took out the unnecessary comparison to true
, because the result of the >
(and other comparison operators) is already a boolean
.
另外,我去掉了对 的不必要的比较true
,因为>
(和其他比较运算符)的结果已经是boolean
.
You can change your else if
condition similarly.
您可以else if
类似地改变您的状况。