无法访问的代码;我不明白为什么它无法访问
Unreachable code ; i dont understand why it is unreachable
import java.util.Scanner;
public class start {
public static void main(String[] args) {
System.out.println("time to pick a name! type in your desired option");
System.out.println("1: Harry");
System.out.println("2: Billybob");
System.out.println("3: Chuckle brother number 2");
Scanner reader = new Scanner(System.in);
byte input = Byte.parseByte(reader.nextLine());
switch (input) {
case 1:
System.out.println("You have selected Harry");
break;
case 2:
System.out.println("You have selected Billybob");
break;
case 3:
System.out.println("You have selected Chuckle Brother number 2");
break;
default:
System.out.println("Invalid Input");
break;
}
System.out
.println("time to head off. select which path you want to take");
System.out.println("1: the path heading north towards Winterfell");
System.out.println("2: the path heading south, towards the red keep");
System.out
.println("3: the path heading East, towards the Moat Caillin");
byte input2 = Byte.parseByte(reader.nextLine());
switch (input2) {
case 1:
System.out
.println("you start for winterfell. the snows start strong on your way, and you die of exposure. Game Over");
break;
case 2:
System.out
.println("you arrive at the red keep. you are attacked and you lose your money. you are now a beggar in fleebottom");
System.out
.println("A magician offers you a position as his assistant. select which option you take");
System.out.println("1: accept the magicians offer.");
System.out.println("2: kindly decline the magicians offer.");
System.out
.println("3: kill the magician and take his possessions.");
byte input3 = Byte.parseByte(reader.nextLine());
switch (input3) {
case 1:
System.out
.println("the magician takes you to his home, and kills you. Game over.");
break;
case 2:
System.out
.println("the magician is angry, he strikes you, and you die of an infected wound. Game over");
break;
case 3:
System.out
.println("the magician has many expensive potions. You use these to get you into a position within the red keep");
System.out
.println("King Tommen asks you to become a member of the small council. he lets you select your position.. select which option you take");
System.out.println("1: become the master of ships.");
System.out.println("2: become the master of coin.");
System.out.println("3: become the master of whispers.");
byte input5 = Byte.parseByte(reader.nextLine());
switch (input5) {
case 1:
System.out
.println("a storm hits a year later and you lose all of the capitals ships. you are dismissed of your position..");
break;
case 2:
System.out
.println("the capital has no money. the iron bank looks to you for their debt that is owed. you are murdered. Game over!");
break;
case 3:
System.out
.println("you find out secrets of highborn lords. you are a trusted acquaintance to the king.");
System.out
.println("the king is dying. he has no sons. he asks you who he should declare as the next king");
System.out.println("1: you should declare myself");
System.out.println("2: you should declare your uncle.");
System.out
.println("3: you should deide for yourself my king.");
byte input4 = Byte.parseByte(reader.nextLine());
switch (input4) {
case 1:
System.out
.println("the lord insults you at dinner. you lose your temper and walk out. the lord has you killed. Game over.");
break;
case 2:
System.out
.println("the lord accepts your decline. he offers you gold to reconsider, as he has a proposition for you.");
break;
case 3:
System.out
.println("you kill the lord, and take Moat Caillin. you marry the daughter of a highborn.");
break;
default:
System.out.println("invalid input");
}
break;
default:
System.out.println("Invalid Input");
break;
}
break;
default:
System.out.println("invalid input");
}
break;
default:
System.out.println("invalid input");
break;
case 3: {
System.out
.println("You head towards Moat Caillin. the journey is swift and you are greeted by the Lord of Moat Caillin");
System.out
.println("The Lord of Moat Caillin offers your his company for dinner. type in your desired option");
System.out.println("1: accept the lords offer.");
System.out.println("2: kindly decline the lords offer.");
System.out.println("3: kill the lord and take his possessions.");
byte input4 = Byte.parseByte(reader.nextLine());
switch (input4) {
case 1:
System.out
.println("the lord insults you at dinner. you lose your temper and walk out. the lord has you killed. Game over.");
break;
case 2:
System.out
.println("the lord accepts your decline. he offers you gold to reconsider, as he has a proposition for you.");
break;
case 3:
System.out
.println("you kill the lord, and take Moat Caillin. you marry the daughter of a highborn.");
break;
default:
System.out.println("invalid input");
}
break;
Default: System.out.println("Invalid Input");
break;
}
}
}
}
"when it runs it says Exception in thread "main" java.lang.Error: 未解决的编译问题:
无法访问的代码
at start.main(start.java:143)"
我真的无法找到它是如何无法到达的,我检查了牙套和各种东西,任何帮助或提示将不胜感激
你提到了 break 语句后的 'Default' 标签,这就是问题所在
case 3: {
System.out
.println("You head towards Moat Caillin. the journey is swift and you are greeted by the Lord of Moat Caillin");
System.out
.println("The Lord of Moat Caillin offers your his company for dinner. type in your desired option");
System.out.println("1: accept the lords offer.");
System.out.println("2: kindly decline the lords offer.");
System.out.println("3: kill the lord and take his possessions.");
byte input4 = Byte.parseByte(reader.nextLine());
switch (input4) {
case 1:
System.out
.println("the lord insults you at dinner. you lose your temper and walk out. the lord has you killed. Game over.");
break;
case 2:
System.out
.println("the lord accepts your decline. he offers you gold to reconsider, as he has a proposition for you.");
break;
case 3:
System.out
.println("you kill the lord, and take Moat Caillin. you marry the daughter of a highborn.");
break;
default:
System.out.println("invalid input");
}
break;
default: System.out.println("Invalid Input");
break;
}
观察最后3个陈述,你没有发现那里有什么可疑的地方吗?
休息; statement 会脱离 case statement 的控制,default 永远不会有机会执行
首先,分析你的代码,
看看块
case 3: {
System.out
.println("You head towards Moat Caillin. the journey is swift and you are greeted by the Lord of Moat Caillin");
System.out
.println("The Lord of Moat Caillin offers your his company for dinner. type in your desired option");
System.out.println("1: accept the lords offer.");
System.out.println("2: kindly decline the lords offer.");
System.out.println("3: kill the lord and take his possessions.");
byte input4 = Byte.parseByte(reader.nextLine());
switch (input4) {
case 1:
System.out
.println("the lord insults you at dinner. you lose your temper and walk out. the lord has you killed. Game over.");
break;
case 2:
System.out
.println("the lord accepts your decline. he offers you gold to reconsider, as he has a proposition for you.");
break;
case 3:
System.out
.println("you kill the lord, and take Moat Caillin. you marry the daughter of a highborn.");
break;
default:
System.out.println("invalid input");
}
break;
Default:
System.out.println("Invalid Input");
break;
}
让我们分析一下这个块,你有的是一堆打印语句,然后是 switch 块,然后是 break。 break 导致 tou 退出此 case 语句,因此 Default
标签后的代码无法访问
import java.util.Scanner;
public class start {
public static void main(String[] args) {
System.out.println("time to pick a name! type in your desired option");
System.out.println("1: Harry");
System.out.println("2: Billybob");
System.out.println("3: Chuckle brother number 2");
Scanner reader = new Scanner(System.in);
byte input = Byte.parseByte(reader.nextLine());
switch (input) {
case 1:
System.out.println("You have selected Harry");
break;
case 2:
System.out.println("You have selected Billybob");
break;
case 3:
System.out.println("You have selected Chuckle Brother number 2");
break;
default:
System.out.println("Invalid Input");
break;
}
System.out
.println("time to head off. select which path you want to take");
System.out.println("1: the path heading north towards Winterfell");
System.out.println("2: the path heading south, towards the red keep");
System.out
.println("3: the path heading East, towards the Moat Caillin");
byte input2 = Byte.parseByte(reader.nextLine());
switch (input2) {
case 1:
System.out
.println("you start for winterfell. the snows start strong on your way, and you die of exposure. Game Over");
break;
case 2:
System.out
.println("you arrive at the red keep. you are attacked and you lose your money. you are now a beggar in fleebottom");
System.out
.println("A magician offers you a position as his assistant. select which option you take");
System.out.println("1: accept the magicians offer.");
System.out.println("2: kindly decline the magicians offer.");
System.out
.println("3: kill the magician and take his possessions.");
byte input3 = Byte.parseByte(reader.nextLine());
switch (input3) {
case 1:
System.out
.println("the magician takes you to his home, and kills you. Game over.");
break;
case 2:
System.out
.println("the magician is angry, he strikes you, and you die of an infected wound. Game over");
break;
case 3:
System.out
.println("the magician has many expensive potions. You use these to get you into a position within the red keep");
System.out
.println("King Tommen asks you to become a member of the small council. he lets you select your position.. select which option you take");
System.out.println("1: become the master of ships.");
System.out.println("2: become the master of coin.");
System.out.println("3: become the master of whispers.");
byte input5 = Byte.parseByte(reader.nextLine());
switch (input5) {
case 1:
System.out
.println("a storm hits a year later and you lose all of the capitals ships. you are dismissed of your position..");
break;
case 2:
System.out
.println("the capital has no money. the iron bank looks to you for their debt that is owed. you are murdered. Game over!");
break;
case 3:
System.out
.println("you find out secrets of highborn lords. you are a trusted acquaintance to the king.");
System.out
.println("the king is dying. he has no sons. he asks you who he should declare as the next king");
System.out.println("1: you should declare myself");
System.out.println("2: you should declare your uncle.");
System.out
.println("3: you should deide for yourself my king.");
byte input4 = Byte.parseByte(reader.nextLine());
switch (input4) {
case 1:
System.out
.println("the lord insults you at dinner. you lose your temper and walk out. the lord has you killed. Game over.");
break;
case 2:
System.out
.println("the lord accepts your decline. he offers you gold to reconsider, as he has a proposition for you.");
break;
case 3:
System.out
.println("you kill the lord, and take Moat Caillin. you marry the daughter of a highborn.");
break;
default:
System.out.println("invalid input");
}
break;
default:
System.out.println("Invalid Input");
break;
}
break;
default:
System.out.println("invalid input");
}
break;
default:
System.out.println("invalid input");
break;
case 3: {
System.out
.println("You head towards Moat Caillin. the journey is swift and you are greeted by the Lord of Moat Caillin");
System.out
.println("The Lord of Moat Caillin offers your his company for dinner. type in your desired option");
System.out.println("1: accept the lords offer.");
System.out.println("2: kindly decline the lords offer.");
System.out.println("3: kill the lord and take his possessions.");
byte input4 = Byte.parseByte(reader.nextLine());
switch (input4) {
case 1:
System.out
.println("the lord insults you at dinner. you lose your temper and walk out. the lord has you killed. Game over.");
break;
case 2:
System.out
.println("the lord accepts your decline. he offers you gold to reconsider, as he has a proposition for you.");
break;
case 3:
System.out
.println("you kill the lord, and take Moat Caillin. you marry the daughter of a highborn.");
break;
default:
System.out.println("invalid input");
}
break;
Default: System.out.println("Invalid Input");
break;
}
}
}
}
"when it runs it says Exception in thread "main" java.lang.Error: 未解决的编译问题: 无法访问的代码
at start.main(start.java:143)"
我真的无法找到它是如何无法到达的,我检查了牙套和各种东西,任何帮助或提示将不胜感激
你提到了 break 语句后的 'Default' 标签,这就是问题所在
case 3: {
System.out
.println("You head towards Moat Caillin. the journey is swift and you are greeted by the Lord of Moat Caillin");
System.out
.println("The Lord of Moat Caillin offers your his company for dinner. type in your desired option");
System.out.println("1: accept the lords offer.");
System.out.println("2: kindly decline the lords offer.");
System.out.println("3: kill the lord and take his possessions.");
byte input4 = Byte.parseByte(reader.nextLine());
switch (input4) {
case 1:
System.out
.println("the lord insults you at dinner. you lose your temper and walk out. the lord has you killed. Game over.");
break;
case 2:
System.out
.println("the lord accepts your decline. he offers you gold to reconsider, as he has a proposition for you.");
break;
case 3:
System.out
.println("you kill the lord, and take Moat Caillin. you marry the daughter of a highborn.");
break;
default:
System.out.println("invalid input");
}
break;
default: System.out.println("Invalid Input");
break;
}
观察最后3个陈述,你没有发现那里有什么可疑的地方吗?
休息; statement 会脱离 case statement 的控制,default 永远不会有机会执行
首先,分析你的代码, 看看块
case 3: {
System.out
.println("You head towards Moat Caillin. the journey is swift and you are greeted by the Lord of Moat Caillin");
System.out
.println("The Lord of Moat Caillin offers your his company for dinner. type in your desired option");
System.out.println("1: accept the lords offer.");
System.out.println("2: kindly decline the lords offer.");
System.out.println("3: kill the lord and take his possessions.");
byte input4 = Byte.parseByte(reader.nextLine());
switch (input4) {
case 1:
System.out
.println("the lord insults you at dinner. you lose your temper and walk out. the lord has you killed. Game over.");
break;
case 2:
System.out
.println("the lord accepts your decline. he offers you gold to reconsider, as he has a proposition for you.");
break;
case 3:
System.out
.println("you kill the lord, and take Moat Caillin. you marry the daughter of a highborn.");
break;
default:
System.out.println("invalid input");
}
break;
Default:
System.out.println("Invalid Input");
break;
}
让我们分析一下这个块,你有的是一堆打印语句,然后是 switch 块,然后是 break。 break 导致 tou 退出此 case 语句,因此 Default
标签后的代码无法访问