所以我写了一个 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.
而且我不确定如何解决这个问题 - 请提供任何帮助。