java - What does Heads cannot be resolved to a variable mean? -


sorry bother you. have been trying create simple java cointoss simulator recently. here code

import java.util.random; import java.util.scanner;   public class cointoss { static string choice; static string answer; static scanner user_input = new scanner( system.in );  public static void main(string[] args){     system.out.print("heads or tails?");     choice = user_input.nextline();     random rand = new random();     int side = rand.nextint(2);     if (side == 0){         answer = heads;         system.out.println("heads");         if (answer == choice) {             system.out.println("you win!");         }         else {             system.out.println("you lose!");         }     }     else if (side == 1){         answer = tails;         system.out.println("tails");         if (answer == choice) {             system.out.println("you win!");         }         else {             system.out.println("you lose!");         }      } } } 

when try build , run error message saying

exception in thread "main" java.lang.error: unresolved compilation problems:  heads cannot resolved variable tails cannot resolved variable 

it means referring non existing variable :

answer = heads; 

you meant :

answer = "heads"; 

btw, should change answer == choice answer.equals(choice) if want string comparison work.


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -