此代码中存在多个问题 - 类型:Missmatch

Sna*_*rre -5 java

所以我写了一个JAVA代码,该代码应该告诉用户如果将另外两个(从列表中)随机选择的颜色组合,他将获得什么颜色.请注意,我是JAVA的新手(以前只用Python编程).

码:

    package ListOfWords;

     public class testListWords {
  public static void main (String[] args) {

      String [] colors = {"red","green","gray","black","blue","yellow"};

      int colorsLength = colors.length;

      int rand1 = (int) (Math.random() * colorsLength);
      int rand2 = (int) (Math.random() * colorsLength);

      while(rand1==rand2){
          int rand2 = (int) (Math.random() * colorsLength);
      }

      String phrase1 = colors[rand1];
      String phrase2 = colors[rand2];       

      while(phrase1 = "green"){

       if (phrase2 = "red") {
          System.out.print("Combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give you the color Brown");
        }
       if (phrase2 = "gray") {
          System.out.print("Combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give you the color Dark Green");
        }
       if (phrase2 = "black") {
          System.out.print("Combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give you the color Black");
        }
        if (phrase2 = "blue") {
            System.out.print("Combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give you the color Brown");
        }

        if (phrase2 = "yellow") {
            System.out.print("Combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give you the color Brown");
        }
        }
    while(phrase1 = "red"){

        if (phrase2 = "green") {
            System.out.print("Combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give you the color Brown");
        }
        if (phrase2 = "gray") {
            System.out.print("Combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give you the color Dark Red");
        }
        if (phrase2 = "black") {
            System.out.print("Combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give you the color Black");
        }
        if (phrase2 = "blue") {
            System.out.print("Combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give you the color Brown");
        }
        if (phrase2 = "yellow") {
            System.out.print("Combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give you the color Brown");
        }
     }
     while(phrase1 = "gray"){

        if (phrase2 = "red") {
        System.out.print("Combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give you the color Dark red");
        }
        if (phrase2 = "green") {
        System.out.print("Combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give you the color Dark Green");
        }
        if (phrase2 = "black") {
        System.out.print("Combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give you the color Black");
        }
        if (phrase2 = "blue") {
        System.out.print("Combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give you the Dark blue");
        }
        if (phrase2 = "yellow") {
        System.out.print("Combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give you the color Green");
        }
     }
     while(phrase1 = "black"){

        if (phrase2 = "red") {
            System.out.print("Combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give you the color Black");
        }
        if (phrase2 = "green") {
            System.out.print("Combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give you the color Black");
        }
        if (phrase2 = "gray") {
            System.out.print("Combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give you the color Black");
        }
        if (phrase2 = "blue") {
            System.out.print("Combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give you the Black");
        }
        if (phrase2 = "yellow") {
            System.out.print("Combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give you the color Black");
        }
     }
     while(phrase1 = "yellow"){

        if (phrase2 = "red") {
            System.out.print("Combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give you the color Brown");
        }
        if (phrase2 = "green") {
            System.out.print("Combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give you the color Blue");
        }
        if (phrase2 = "gray") {
            System.out.print("Combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give you the color Green");
        }
        if (phrase2 = "blue") {
            System.out.print("Combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give you the Green");
        }
        if (phrase2 = "black") {
            System.out.print("Combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give you the color Black");
        }
     }
     while(phrase1 = "Blue"){

        if (phrase2 = "red") {
            System.out.print("Combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give you the color Brown");
        }
        if (phrase2 = "green") {
            System.out.print("Combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give you the color Brown");
        }
        if (phrase2 = "gray") {
            System.out.print("Combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give you the Dark blue");
        }
        if (phrase2 = "yellow") {
            System.out.print("Combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give you the Green");
        }
        if (phrase2 = "black") {
            System.out.print("Combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give you the color Black");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

}

所以我收到一条错误消息说:

    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    Duplicate local variable rand2
    Type mismatch: cannot convert from String to Boolean.
Run Code Online (Sandbox Code Playgroud)

我不知道如何解决这个问题 - 请帮助.

bas*_*bas 5

首先=是赋值,必须==用作布尔运算符.

其次,要比较Java中的字符串,您需要equals()在字符串上调用该方法.

if ("red".equals(phrase2)) {
    // logic here
}
Run Code Online (Sandbox Code Playgroud)

第三,你分配了两次rand2.您必须第二次使用另一个变量,或者必须删除该int指示.

int rand2 = (int) (Math.random() * colorsLength);

while(rand1==rand2){
    rand2 = (int) (Math.random() * colorsLength);
}
Run Code Online (Sandbox Code Playgroud)