-5

所以我写了一个 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");
        }
    }
}

}

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

    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    Duplicate local variable rand2
    Type mismatch: cannot convert from String to Boolean.

而且我不确定如何解决这个问题 - 请提供任何帮助。

4

5 回答 5

5

首先=是赋值,你必须使用==布尔运算符。

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

if ("red".equals(phrase2)) {
    // logic here
}

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

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

while(rand1==rand2){
    rand2 = (int) (Math.random() * colorsLength);
}
于 2013-07-29T18:12:51.927 回答
1

您遇到的一个问题是:

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

你已经声明了rand2,所以不要再这样做了。您应该将其更改为:

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

另一件事是,您将赋值运算符 ( =) 与相等运算符 ( )混合在一起==。前者为变量赋值,后者检查变量是否相等。

当您尝试匹配变量时,请使用==.

最后,在 Java 中,您不能使用相等运算符来检查字符串 - 这将逐个匹配一个对象并查看它们是否是同一个对象。您必须改用该equals方法,如下所示:

while(phrase1.equals("green"))

而不是这个:

while(phrase1 = "green")

甚至while(phrase1 == "green")(适用于整数/布尔值/其他类型)。

于 2013-07-29T18:17:03.453 回答
0

为了在 Java 中比较字符串,您必须使用 equals() 方法使用"red".equals(phrase2)"red".equalsIgnoreCase(phrase2)

==Java中的运算符用于检查相等性,=是一个赋值运算符。但是Java中的字符串是对象,String.class所以==进行引用检查而不是值检查。

因此,要比较 Java 中的两个对象,请使用equals()方法。

于 2013-07-29T18:13:45.540 回答
0

这里有一个语法错误:

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

  while(rand1==rand2){
      //int rand2 = (int) (Math.random() * colorsLength); - wrong
      //rand2 is already defined, you can write only
      rand2 = (int) (Math.random() * colorsLength);
  }

另外,如果你想要 2 个不同的随机数,你可以:

  int rand1 = (int) (Math.random() * colorsLength);
  int rand2 = (int) (Math.random() * (colorsLength-1));
  if(rand2 >= rand1) rand2++;
于 2013-07-29T18:16:39.770 回答
0

好的。你在这里写的东西有很多问题。我会尽力帮助您解决其中的一些问题。

首先,正如 bas 指出的那样,一般来说,如果你想比较两个值是否相等,你使用 ==,而不是 =。例如:

if ( x == y ) {
  //DO STUFF
}

其次,bas 也指出了这一点,在 Java 中你不能使用 == 来比较字符串。你需要做的是:

if ( string1.equals(string2) ) {
  //DO STUFF
}

第三。“尽管”。这会给你带来很多问题。While 意味着继续执行后面的代码,直到其测试为假。所以,当你这样做时:

while ( myString.equals( "bananas" ) ) {
  System.out.println ( "Lots of bananas." );
}

如果字符串是“bananas”,那么你会得到这个:

Lots of bananas.
Lots of bananas.
Lots of bananas.
Lots of bananas.
Lots of bananas.
Lots of bananas.
Lots of bananas.
Lots of bananas.
....

永远。你真正想要使用的是另一个 if。实际上,这里也有一个方便的逻辑。您可以修改语句以使用 else ifs,而不是一堆 if,因为所有这些选项都是互斥的:

if ( string1.equals("yellow")) {
  if ( string2.equals("green")) {
    System.out.println("stuff");
  }
  else if ( string2.equals("blue")) {
    System.out.println("more stuff");
  }
}
else if ( string1.equals("green")) {
  if ( string2.equals("blue")) {
    System.out.println("lalala");
  }
  else if ( string2.equals("yellow")) {
    System.out.println("more stuff");
  }
}

还有一些更好的方法来编写这个程序,这些方法涉及更少的代码重复和最终更简单的逻辑,但为了避免让你超负荷,现在这可能就足够了。

于 2013-07-29T18:26:23.437 回答