The operator > is undefined for the argument type(s) boolean, double

混江龙づ霸主 提交于 2019-12-02 09:43:30
if (firstNumber > secondNumber > thirdNumber == true)

This is invalid; you want:

if (firstNumber > secondNumber && secondNumber > thirdNumber)

The same goes for your else if condition.

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.


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).


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]);

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.

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.

You can change your else if condition similarly.

You can't have this : if (firstNumber > secondNumber > thirdNumber == true).

You need to break it down to:

if (firstNumber > secondNumber && secondNumber > thirdNumber) {
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!