質問

I was trying to get an infinite addition calculation .When I compile this code I get unreachable statement that I believe it been caused by different statement levels .

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

}

The unreachable statement is c=a+c;

役に立ちましたか?

解決

You are not breaking the outer while loop after successful input by user. So the control after these statements:

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

will reach the for loop again and again. And hence the statement after the for loop is unreachable, as the loop will now run infinitely.

Add a break after the outer while loop finishes:

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

BTW, you are complicating your task too much by having those infinite for loops. You should read the first number outside the loop, and then add a while loop to read more inputs from user:

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

Apart from that, you should also validate the input whether it's really an integer, before invoking input.nextInt() method, which will blow if user passes "abc". For that use input.hasNextInt() method. I leave that task to you.

他のヒント

You are using infinite for loop. So this is obvious that program will never reach to c = a+c;statement.

Unreachable code is the code which will never be called. In you code the line 'c=a+c;' will never be called since its outside the infinite loop

The code is will never be run, because it's put after a infinite loop that never ends.

I would advise to indent your code a bit more, always have '}' curly brackets in a new line so you can see this issue.

To get rid of the error you can add a 'break Bag2;' after 'System.out.println("The result is :" + c);', something like:

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

}

Though I believe the fundamental logic of your code is wrong. For a good example of the scanner, see: http://web.eecs.utk.edu/~bvz/cs365/examples/datacheck.html

Also as a general practice you should try to spot using so many while, break, continue, infinite loop in your code.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top