【问题标题】:Java unreachable statement caused by different statement levels语句级别不同导致的Java不可达语句
【发布时间】:2013-09-27 22:47:00
【问题描述】:

我试图进行无限加法计算。当我编译此代码时,我得到无法访问的语句,我认为它是由不同的语句级别引起的。

import java.util.Scanner;
public class infiAdd {
    public static void main (String [] args ){

    int a;
    char ans;
    int c=0; 
    Scanner input;
    input=new Scanner(System.in);
    Bag: for(;;) {
        System.out.print("Please enter a number:"); 
        a=input.nextInt(); 
        Bag2:for(;;) {
        System.out.println("Do you wnat to add more ?[y/n]"); 
        ans=input.next().charAt(0); 
        while (!(ans=='y')) {
            while(!(ans=='n')){
            System.out.println("Please enter a valid answer!");
            continue Bag2;
            }
            c=c+a;
            System.out.println("The result is :"+c);
        }}


        c=a+c;

        continue Bag;}  
    }

}

无法访问的语句是c=a+c;

【问题讨论】:

  • 哪个语句被标记为“无法访问”?
  • 如果您按照惯例缩进并使用空格而不是制表符,阅读代码会更容易。

标签: java unreachable-statement


【解决方案1】:

在用户成功输入后,您不会中断外部 while 循环。所以这些语句之后的控制:

c=c+a;
System.out.println("The result is :"+c);

将一次又一次地到达for 循环。因此,for 循环之后的语句无法访问,因为循环现在将无限运行。

在外部while循环完成后添加break

    c=c+a;
    System.out.println("The result is :"+c);
}  // While loop ends
break;

顺便说一句,那些无限的for 循环使您的任务变得过于复杂。您应该读取loop 之外的第一个数字,然后添加while 循环以读取来自用户的更多输入:

System.out.print("Please enter a number:"); 
a=input.nextInt();

while (true) {
    System.out.println("Do you want to add more: [y/n]");
    ans=input.next().charAt(0);

    if (ans == 'n' || ans == 'N') break;
    if (ans == 'y' || ans == 'Y') {
        System.out.print("Please enter a number:"); 
        int c = input.nextInt();
        a += c;
        continue;
    } 
    System.out.println("Please enter a valid option: [y/n]");
    continue;
}

System.out.println("The result is :"+c);

除此之外,在调用input.nextInt() 方法之前,您还应该验证输入是否真的是integer,如果用户通过"abc",它将崩溃。为此使用input.hasNextInt() 方法。我把这个任务留给你。

【讨论】:

    【解决方案2】:

    您正在使用无限循环。所以很明显,程序永远不会到达c = a+c;statement。

    【讨论】:

      【解决方案3】:

      无法访问的代码是永远不会被调用的代码。在你的代码行'c = a + c;'永远不会被调用,因为它在无限循环之外

      【讨论】:

        【解决方案4】:

        代码永远不会运行,因为它被置于一个永不结束的无限循环之后。

        我会建议你的代码缩进一点,总是在新行中有'}'大括号,这样你就可以看到这个问题。

        要消除错误,您可以添加“break Bag2;”在 'System.out.println("The result is :" + c);' 之后,类似:

        public class InfiniteAdd {
            public static void main(String[] args) {
        
                int a;
                char ans;
                int c = 0;
                Scanner input;
                input = new Scanner(System.in);
                Bag: for (;;) {
                    System.out.print("Please enter a number:");
                    a = input.nextInt();
                    Bag2: for (;;) {
                        System.out.println("Do you wnat to add more ?[y/n]");
                        ans = input.next().charAt(0);
                        while (!(ans == 'y')) {
                            while (!(ans == 'n')) {
                                System.out.println("Please enter a valid answer!");
                                continue Bag2;
                            }
                        }
                        c = c + a;
                        System.out.println("The result is :" + c);
                        break Bag2;
                    }
        
                    c = a + c;
        
                    continue Bag;
                }
            }
        
        }
        

        虽然我认为您的代码的基本逻辑是错误的。 有关扫描仪的一个很好的示例,请参阅:http://web.eecs.utk.edu/~bvz/cs365/examples/datacheck.html

        此外,作为一般做法,您应该尝试在代码中使用如此多的 while、break、continue 和无限循环。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-01-01
          • 2013-10-18
          • 2013-12-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-23
          • 2018-10-20
          相关资源
          最近更新 更多