所以我编写了一个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.
我不知道如何解决这个问题 - 请帮助。
答案 0 :(得分: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);
}
答案 1 :(得分: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")
(适用于整数/布尔/其他类型)。
答案 2 :(得分:0)
为了比较Java中的字符串,您必须使用equals()方法使用"red".equals(phrase2)
或"red".equalsIgnoreCase(phrase2)
==
运算符用于检查相等性,=
是赋值运算符。但是Java中的字符串是String.class
的对象所以==做引用检查而不是值检查。
因此,要比较Java中的两个对象,请使用equals()
方法。
答案 3 :(得分: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++;
答案 4 :(得分:0)
好的。你在这里写的东西有很多问题。我会尽力帮助你解决其中一些问题。
首先,正如bas指出的那样,一般来说,如果你想比较两个值以查看它们是否相等,你可以使用==,而不是=。例如:
if ( x == y ) {
//DO STUFF
}
其次,bas也指出了这一点,在Java中你不能使用==来比较字符串。你需要做的是:
if ( string1.equals(string2) ) {
//DO STUFF
}
第三。 “而”。这个会给你带来很多问题。虽然意味着继续执行下面的代码,直到其测试为假。所以,当你这样做时:
while ( myString.equals( "bananas" ) ) {
System.out.println ( "Lots of 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。实际上,这里也有一个方便的逻辑。您可以修改语句以使用其他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");
}
}
还有一些更好的编写程序的方法,它涉及更少的代码重复和最终更简单的逻辑,但为了避免使你超载,这可能就足够了。