为什么我得到错误无法访问的声明?

时间:2011-08-06 12:44:45

标签: java switch-statement

为什么我会收到第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") ;  
    }
}

4 个答案:

答案 0 :(得分:8)

因为您break就在它之前。

答案 1 :(得分:6)

由于第88行的break;声明:

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



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

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

答案 2 :(得分:1)

中断后你的交换机块中有代码,但没有前面的情况,这是无法访问的代码。

但严重的是,这段代码已经过去了,应该废弃。我建议你重新编写这个并使用数组来简化,在编写之前计划你要编写的内容,并且一次只增加一小部分代码,在添加每一段代码后编译并修复任何和所有代码添加任何新代码之前的编译错误。

答案 3 :(得分:0)

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

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

}