Original Post
How come this doesn't work in Java?
public String newSuit () {
int pickSuit = (int)Math.random() * 4;
switch (pickSuit) {
case 0: return "Club";
case 1: return "Diamond";
case 2: return "Heart";
case 3: return "Spade";
}
}
The error I get is: missing return statement .
I replaced it with this:
public String newSuit () {
int pickSuit = (int)Math.random() * 4;
String randomSuit = null;
switch (pickSuit) {
case 0: randomSuit = "Club"; break;
case 1: randomSuit = "Diamond"; break;
case 2: randomSuit = "Heart"; break;
case 3: randomSuit = "Spade"; break;
}
return randomSuit;
}
And it works, but I would still like to know why the compiler chokes when I use the first piece of code. I don't believe it should!!
[edited by - Alpha_ProgDes on April 26, 2004 7:55:02 PM]
[edited by - Alpha_ProgDes on April 26, 2004 7:57:45 PM]