So I'm trying to learn Java but errors like this are not helping :/ I was following the tutorial and then I got compilation error. I tried in many ways but nothing helps :( Here is example code of what does NOT compile on my Eclipse:
public class coin {
public static void main(String[] args){
Random rand = new Random();
int result = rand.nextInt(2);
if (result == 0){
System.out.println("heads");
}
else if(result == 1){
System.out.println("tails");
}
else if(result == 3){
System.out.println("side. fix your random number generator");
}
}
}
It gives me error:
"Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method nextInt(int) is undefined for the type Random at coin.main(coin.java:8)"
I give up - I'm out of ideas. Please help ;_;
EDIT: Yes, I tried Ctrl + Shift + O
EDIT 2: Now I get what was my problem. I was trying to import random with () at the end... "import java.util.Random();" That's why it didn't work - it's fine now :)
Aucun commentaire:
Enregistrer un commentaire