为什么我会收到错误无法访问的语句?
为什么我会收到第 92 行是无法访问的语句的错误?基本上我想做的是跳过一些代码,如 21-22-23... 如果用户输入类似 11-12-13...
import java.util.*;
public class Words {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out. print ("\n\tINTEGER TO WORDS") ;
System.out.print ("\n\nEnter a number between 1 and 9999: ") ;
int num = s.nextInt();
System.out. print ("\nThe answer is: ") ;
int tho = (num - (num % 1000)) ;
int h = (num - tho - (num % 100)) ;
int t = (num - tho - h - (num % 10)) ;
int o = (num - tho - h - t) ;
switch (tho)
{
case 9000:
System.out.print ("Nine Thousand ") ;
break ;
case 8000:
System.out.print ("Eight Thousand ") ;
break ;
case 7000:
System.out.print ("Seven Thousand ") ;
break ;
case 6000:
System.out.print ("Six Thousand ") ;
break ;
case 5000:
System.out.print ("Five Thousand ") ;
break ;
case 4000:
System.out.print ("Four Thousand ") ;
break ;
case 3000:
System.out.print ("Three Thousand ") ;
break ;
case 2000:
System.out.print ("Two Thousand ") ;
break ;
case 1000:
System.out.print ("One Thousand ") ;
break ;
}
switch (h)
{
case 100:
System.out.print ("One Hundred ") ;
break ;
case 200:
System.out.print ("Two Hundred ") ;
break ;
case 300:
System.out.print ("Three Hundred ") ;
break ;
case 400:
System.out.print ("Four Hundred ") ;
break ;
case 500:
System.out.print ("Five Hundred ") ;
break ;
case 600:
System.out.print ("Six Hundred ") ;
break ;
case 700:
System.out.print ("Seven Hundred ") ;
break ;
case 800:
System.out.print ("Eight Hundred ") ;
break ;
case 900:
System.out.print ("Nine Hundred ") ;
break ;
}
switch (t)
{
case 10:
System.out.print ("Ten ") ;
break ;
**switch (o)**
{
case 11:
System.out.print ("Eleven ") ;
break ;
case 12:
System.out.print ("Twelve ") ;
break ;
case 13:
System.out.print ("Thirteen ") ;
break ;
case 14:
System.out.print ("Fourteen ") ;
break ;
case 15:
System.out.print ("Fifteen ") ;
break ;
case 16:
System.out.print ("Sixteen ") ;
break ;
case 17:
System.out.print ("Seventeen ") ;
break ;
case 18:
System.out.print ("Eighteen ") ;
break ;
case 19:
System.out.print ("Nineteen ") ;
break ;
}
case 20:
System.out.print ("Twenty ") ;
break ;
case 30:
System.out.print ("Thirty") ;
break ;
case 40:
System.out.print ("Fourty ") ;
break ;
case 50 :
System.out.print ("Fifty ") ;
break ;
case 60:
System.out.print ("Sixty ") ;
break ;
case 70:
System.out.print ("Seventy ") ;
break ;
case 80:
System.out.print ("Eighty ") ;
break ;
case 90:
System.out.print ("Ninenty ") ;
break ;
}
switch (o)
{
case 1:
System.out.print ("One ") ;
break ;
case 2:
System.out.print ("Two ") ;
break ;
case 3:
System.out.print ("Three ") ;
break ;
case 4:
System.out.print ("Four ") ;
break ;
case 5:
System.out.print ("Five ");
break ;
case 6:
System.out.print ("Six ");
break ;
case 7:
System.out.print ("Seven ");
break ;
case 8:
System.out.print ("Eight ");
break ;
case 9:
System.out.print ("Nine ");
break ;
}
System.out. print ("\n") ;
}
}
Why do I get the error that line 92 is an unreachable statement? Basically what I'm trying to do is to skip some of the codes like 21-22-23... if the user inputs something like 11-12-13...
import java.util.*;
public class Words {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out. print ("\n\tINTEGER TO WORDS") ;
System.out.print ("\n\nEnter a number between 1 and 9999: ") ;
int num = s.nextInt();
System.out. print ("\nThe answer is: ") ;
int tho = (num - (num % 1000)) ;
int h = (num - tho - (num % 100)) ;
int t = (num - tho - h - (num % 10)) ;
int o = (num - tho - h - t) ;
switch (tho)
{
case 9000:
System.out.print ("Nine Thousand ") ;
break ;
case 8000:
System.out.print ("Eight Thousand ") ;
break ;
case 7000:
System.out.print ("Seven Thousand ") ;
break ;
case 6000:
System.out.print ("Six Thousand ") ;
break ;
case 5000:
System.out.print ("Five Thousand ") ;
break ;
case 4000:
System.out.print ("Four Thousand ") ;
break ;
case 3000:
System.out.print ("Three Thousand ") ;
break ;
case 2000:
System.out.print ("Two Thousand ") ;
break ;
case 1000:
System.out.print ("One Thousand ") ;
break ;
}
switch (h)
{
case 100:
System.out.print ("One Hundred ") ;
break ;
case 200:
System.out.print ("Two Hundred ") ;
break ;
case 300:
System.out.print ("Three Hundred ") ;
break ;
case 400:
System.out.print ("Four Hundred ") ;
break ;
case 500:
System.out.print ("Five Hundred ") ;
break ;
case 600:
System.out.print ("Six Hundred ") ;
break ;
case 700:
System.out.print ("Seven Hundred ") ;
break ;
case 800:
System.out.print ("Eight Hundred ") ;
break ;
case 900:
System.out.print ("Nine Hundred ") ;
break ;
}
switch (t)
{
case 10:
System.out.print ("Ten ") ;
break ;
**switch (o)**
{
case 11:
System.out.print ("Eleven ") ;
break ;
case 12:
System.out.print ("Twelve ") ;
break ;
case 13:
System.out.print ("Thirteen ") ;
break ;
case 14:
System.out.print ("Fourteen ") ;
break ;
case 15:
System.out.print ("Fifteen ") ;
break ;
case 16:
System.out.print ("Sixteen ") ;
break ;
case 17:
System.out.print ("Seventeen ") ;
break ;
case 18:
System.out.print ("Eighteen ") ;
break ;
case 19:
System.out.print ("Nineteen ") ;
break ;
}
case 20:
System.out.print ("Twenty ") ;
break ;
case 30:
System.out.print ("Thirty") ;
break ;
case 40:
System.out.print ("Fourty ") ;
break ;
case 50 :
System.out.print ("Fifty ") ;
break ;
case 60:
System.out.print ("Sixty ") ;
break ;
case 70:
System.out.print ("Seventy ") ;
break ;
case 80:
System.out.print ("Eighty ") ;
break ;
case 90:
System.out.print ("Ninenty ") ;
break ;
}
switch (o)
{
case 1:
System.out.print ("One ") ;
break ;
case 2:
System.out.print ("Two ") ;
break ;
case 3:
System.out.print ("Three ") ;
break ;
case 4:
System.out.print ("Four ") ;
break ;
case 5:
System.out.print ("Five ");
break ;
case 6:
System.out.print ("Six ");
break ;
case 7:
System.out.print ("Seven ");
break ;
case 8:
System.out.print ("Eight ");
break ;
case 9:
System.out.print ("Nine ");
break ;
}
System.out. print ("\n") ;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
因为你在它之前
中断
。Because you
break
right before it.由于第 88 行的
break;
语句:您告诉程序“跳过此 switch 语句中该表达式下面的所有内容,直到跳出 switch 语句”,并且
break
是无条件的,这意味着它在case 10
内总是发生。编译器足够聪明,可以识别这意味着switch(o)
永远无法执行,因此它会警告您“无法访问代码”。Because of the
break;
statement on line 88:You're telling the program "skip everything below this expression inside this switch statement until you're out of the switch statement", and the
break
is unconditional, meaning it will always occur when insidecase 10
. The compiler is smart enough to recognize that this means,switch(o)
can never execute, and so it alerts you that the "code cannot be reached".在中断之后,您在 switch 块中有代码,但没有前面的情况,这是无法访问的代码。
但说真的,这段代码很无聊,应该被废弃。我建议您重新编写此代码并使用数组来简化,在编写之前计划好要编写的内容,并且一次仅增量添加少量代码,在添加每一位代码后进行编译并修复所有代码添加任何新代码之前出现编译错误。
You've got code in a switch block after a break but without a preceding case, and that is unreachable code.
But seriously, this code is borked and should be scrapped. I recommend you re-write this and use arrays to simplify, plan what you're going to write before you write it and only incrementally add small bits of code at a time, compiling after each bit of code is added and fixing any and all compilation errors before adding any new code.
这是您修改过的代码并进行了测试。
导入 java.util.*;
公共课单词 {
}
Here is your little modified code and tested also.
import java.util.*;
public class Words {
}