为什么我会收到错误无法访问的语句?

发布于 2024-11-28 00:30:27 字数 5332 浏览 0 评论 0原文

为什么我会收到第 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

荭秂 2024-12-05 00:30:27

因为你在它之前中断

Because you break right before it.

柒七 2024-12-05 00:30:27

由于第 88 行的 break; 语句:

case 10:
                System.out.print ("Ten ") ;
                break ; // <-- problem is here



                **switch (o)** 
                {
                    case 11:
                    System.out.print ("Eleven ") ;

您告诉程序“跳过此 switch 语句中该表达式下面的所有内容,直到跳出 switch 语句”,并且 break 是无条件的,这意味着它在 case 10总是发生。编译器足够聪明,可以识别这意味着 switch(o) 永远无法执行,因此它会警告您“无法访问代码”。

Because of the break; statement on line 88:

case 10:
                System.out.print ("Ten ") ;
                break ; // <-- problem is here



                **switch (o)** 
                {
                    case 11:
                    System.out.print ("Eleven ") ;

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 inside case 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".

笑咖 2024-12-05 00:30:27

在中断之后,您在 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.

谁对谁错谁最难过 2024-12-05 00:30:27

这是您修改过的代码并进行了测试。

导入 java.util.*;

公共课单词 {

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=0;
    int o=0;
    if(num>10 && num<20)
    {
        t=num;
    }else{
        t = (num - tho - h -  (num % 10)) ;

         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 ;

                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") ;  
}

}

Here is your little modified code and tested also.

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=0;
    int o=0;
    if(num>10 && num<20)
    {
        t=num;
    }else{
        t = (num - tho - h -  (num % 10)) ;

         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 ;

                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") ;  
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文