所以我正在为我的Java类做这个任务,我一直遇到这个错误"unreachable code"
,我无法找到如何解决这个错误,甚至不知道为什么会发生这个错误。请帮忙!
public static void main(String[] args)
{
int suit;
int cardNumber;
Random generator = new Random();
suit = generator.nextInt (4)+1;;
String suitString;
switch (suit) {
case 1: suitString = "hearts!";
break;
case 2: suitString = "diamonds!";
break;
case 3: suitString = "clubs!";
break;
case 4: suitString = "spades!";
break;
cardNumber = generator.nextInt (13) +1;; //unreachable code
String cardNumberString;
switch (cardNumber) {
case 1: cardNumberString = "ace";
break;
case 2: cardNumberString = "2";
break;
case 3: cardNumberString = "3";
break;
case 4: cardNumberString = "4";
break;
case 5: cardNumberString = "5";
break;
case 6: cardNumberString = "6";
break;
case 7: cardNumberString = "7";
break;
case 8: cardNumberString = "8";
break;
case 9: cardNumberString = "9";
break;
case 10: cardNumberString = "10";
break;
case 11: cardNumberString = "11";
break;
case 12: cardNumberString = "12";
break;
case 13: cardNumberString = "13";
break;
答案 0 :(得分:5)
第一个switch
之后,case 4
语句没有结束括号。下一个陈述是在break
之后,这意味着它无法到达。
switch (suit) {
case 1: suitString = "hearts!";
break;
case 2: suitString = "diamonds!";
break;
case 3: suitString = "clubs!";
break;
case 4: suitString = "spades!";
break;
} // Need to close the switch statement
答案 1 :(得分:2)
您必须关闭括号。 case 4
{
未关闭后,代码无法访问。
switch (suit) {
case 1: suitString = "hearts!";
break;
case 2: suitString = "diamonds!";
break;
case 3: suitString = "clubs!";
break;
case 4: suitString = "spades!";
break;
} //Here