二元运算符“&&”的错误操作数。请帮我找出问题所在

Bad operands for binary operator '&&'. Please Help me find out what's wrong

 package whowins;

import java.util.Scanner;
public class WhoWins {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    String candidate[] = {"Trump","Fiorina","Carson","Sanders"};
    String position[] = {"trumpPosition", "fiorinaPosition", "carsonPosition", "sandersPosition"};
    double supportTrump, supportFiorina, supportCarson, supportSanders;

    Scanner cs = new Scanner(System.in);

    System.out.println("What is Trump's support?");
    supportTrump = cs.nextDouble();
    System.out.println("What is Fiorina's Support?");
    supportFiorina = cs.nextDouble();
    System.out.println("What is Carson's support?");
    supportCarson = cs.nextDouble();
    System.out.println("What is Sander's support?");
    supportSanders = cs.nextDouble();

    if ((supportTrump) > (supportFiorina) && (supportCarson) && (supportSanders) {
        position[0] = ("First Place");
    }
}
}

我遇到的问题是无法比较用户将提供的双打。

if ((supportTrump) > (supportFiorina) && (supportCarson) && (supportSanders) { <--------这一整行都是错误的,我已经尝试了所有方法。

supportCarsonsupportSanders 不是布尔值,因此它们不能作为逻辑与的操作数。

也许你打算写这个:

if  (supportTrump > supportFiorina && supportCarson > supportSanders)

或者这个:

if  (supportTrump > supportFiorina && supportTrump > supportCarson &&  supportTrump > supportSanders)

if 语句如何工作?

if (condition){
   statement
}

其中 condition 是 returns 真或假的表达式。

现在让我们来分析一下您的代码。

if ((supportTrump) > (supportFiorina) && (supportCarson) && (supportSanders) {
    position[0] = ("First Place");
}

在此示例中,条件为 (supportTrump) > (supportFiorina) && (supportCarson) && (supportSanders)。 Java 中的 && 是逻辑运算符,表示 AND 或只是连词。因此,您的 if 语句中有 3 个简单条件:

  1. supportTrump > supportFiorina(类型:布尔值)
  2. supportCarson(类型:双)
  3. supportSanders(类型:双)

所有这些简单的条件都必须是布尔类型。第一个条件是布尔值。但是另外 2 个 returns 双类型值。 如果你想说 "Execute statement in brackets if supportTrump greater than supportFiorina, supportCarson and supportSanders" 你应该替换这一行:

if ((supportTrump) > (supportFiorina) && (supportCarson) && (supportSanders) 

有了这个

 if (supportTrump > supportFiorina && supportTrump > supportCarson && supportTrump > supportSanders)

你得再对比一下。 &&(supportCarson) 实际上并没有比较它。所以 &&(supportTrump>supportCarson) 会起作用。