java - Multiple problems in this code - Type: Missmatch -
so have written java code supposed tell user color if combining 2 other (from list) random selected colors. mind new java (have programmed in python before).
code:
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 color brown"); } if (phrase2 = "gray") { system.out.print("combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give color dark green"); } if (phrase2 = "black") { system.out.print("combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give color black"); } if (phrase2 = "blue") { system.out.print("combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give color brown"); } if (phrase2 = "yellow") { system.out.print("combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give color brown"); } } while(phrase1 = "red"){ if (phrase2 = "green") { system.out.print("combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give color brown"); } if (phrase2 = "gray") { system.out.print("combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give color dark red"); } if (phrase2 = "black") { system.out.print("combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give color black"); } if (phrase2 = "blue") { system.out.print("combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give color brown"); } if (phrase2 = "yellow") { system.out.print("combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give color brown"); } } while(phrase1 = "gray"){ if (phrase2 = "red") { system.out.print("combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give color dark red"); } if (phrase2 = "green") { system.out.print("combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give color dark green"); } if (phrase2 = "black") { system.out.print("combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give color black"); } if (phrase2 = "blue") { system.out.print("combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give dark blue"); } if (phrase2 = "yellow") { system.out.print("combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give color green"); } } while(phrase1 = "black"){ if (phrase2 = "red") { system.out.print("combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give color black"); } if (phrase2 = "green") { system.out.print("combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give color black"); } if (phrase2 = "gray") { system.out.print("combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give color black"); } if (phrase2 = "blue") { system.out.print("combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give black"); } if (phrase2 = "yellow") { system.out.print("combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give color black"); } } while(phrase1 = "yellow"){ if (phrase2 = "red") { system.out.print("combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give color brown"); } if (phrase2 = "green") { system.out.print("combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give color blue"); } if (phrase2 = "gray") { system.out.print("combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give color green"); } if (phrase2 = "blue") { system.out.print("combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give green"); } if (phrase2 = "black") { system.out.print("combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give color black"); } } while(phrase1 = "blue"){ if (phrase2 = "red") { system.out.print("combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give color brown"); } if (phrase2 = "green") { system.out.print("combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give color brown"); } if (phrase2 = "gray") { system.out.print("combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give dark blue"); } if (phrase2 = "yellow") { system.out.print("combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give green"); } if (phrase2 = "black") { system.out.print("combining" + " " + phrase1 + " " + "with" + " " + phrase2 + " " + "will give color black"); } } }
}
so im getting error message saying:
exception in thread "main" java.lang.error: unresolved compilation problems: duplicate local variable rand2 type mismatch: cannot convert string boolean.
and im not sure how fix - please.
alright. there number of problems have written here. try address of them.
first off, bas pointed out, in general, if want compare 2 values see if equal, use ==, not =. example:
if ( x == y ) { //do stuff }
second, bas pointed out, in java can't use == compare strings. need is:
if ( string1.equals(string2) ) { //do stuff }
third. "while". 1 going cause lots of issues you. while means keep doing code follows, until test false. so, when this:
while ( mystring.equals( "bananas" ) ) { system.out.println ( "lots of bananas." ); }
if string "bananas", you'll this:
lots of bananas. lots of bananas. lots of bananas. lots of bananas. lots of bananas. lots of bananas. lots of bananas. lots of bananas. ....
forever. want use if. actually, there convenient piece of logic here well. instead of bunch of if's, can modify statement use else ifs, since of these options mutually exclusive:
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"); } }
there better ways of writing program involve lot less code duplication , simpler logic, avoid overloading you, sufficient now.
Comments
Post a Comment