0

我正在尝试使用嵌套的 switch 语句。是否可以在嵌套的 switch 语句中使用不同的表达式类型?

我收到以下编译器错误: arith cannot be resolved as variable.

有一条注释指出错误发生的位置。

这是我的代码供您参考:

import java.util.Arrays;
import java.util.Scanner;


class Choice1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
                @SuppressWarnings("resource")

                Scanner S = new Scanner(System.in);

                int Source = 0, Target = 0,codePos = 0;

                System.out.println("Enter the Source(1-2) :");
                Source = S.nextInt();

                System.out.println("Enter the Target Value (1 - 3) :");
                Target = S.nextInt();


                if (Source == 1)
                {
                    String[] arith = {"Add","Sub"};
                    codePos = Arrays.binarySearch(arith, 0, arith.length, Target);
                }
                else if (Source == 2)
                {
                    String[] Multi = {"Multiplication","Division","Modulas"};
                    codePos = Arrays.binarySearch(Multi, 0, Multi.length, Target);
                }
                else
                {
                    System.out.println("Invalid Value");
                }

                switch (Source) {
                case 1:
                    switch (arith[codePos]) {  // <============= !! ERROR HERE !! 
                    case "Add":
                        System.out.println("Addition....!!!");
                        int a,b,c;

                        System.out.println("Enter value for A :");
                        a = S.nextInt();
                        System.out.println("Enter value for B :");
                        b = S.nextInt();

                        c = a + b;

                        System.out.println("The Result " + c);

                        break;
                    case "Sub":
                        System.out.println("Subtraction....!!!");
                        int d,e,f;

                        System.out.println("Enter value for D :");
                        d = S.nextInt();
                        System.out.println("Enter value for E :");
                        e = S.nextInt();

                        f = d - e;

                        System.out.println("The Result" + f);

                        break;
                    default:
                        System.out.println("Invalid Value...!!!");

                    }
                    break;

                case 2:
                    switch (Target) {
                    case 1:
                        System.out.println("multiplication....!!!");
                        int a,b,c;

                        System.out.println("Enter value for A :");
                        a = S.nextInt();
                        System.out.println("Enter value for B :");
                        b = S.nextInt();

                        c = a * b;

                        System.out.println("The Result" + c);

                        break;
                    case 2:
                        System.out.println("Division....!!!");
                        int d,e,f;

                        System.out.println("Enter value for D :");
                        d = S.nextInt();
                        System.out.println("Enter value for E :");
                        e = S.nextInt();

                        f = d / e;

                        System.out.println("The Result" + f);

                        break;

                    case 3:
                        System.out.println("Modulas....!!!");
                        int g,h,i;

                        System.out.println("Enter value for G :");
                        g = S.nextInt();
                        System.out.println("Enter value for H :");
                        h = S.nextInt();

                        i = g % h;

                        System.out.println("The Result" + i);

                        break;
                    default:
                        System.out.println("Invalid Value...!!!");

                    }
                    break;
                }

            }
        }

    }

}
4

1 回答 1

2

您的问题是范围问题:您arith在“if”内部定义,然后尝试在if语句之外使用它。每当您打开花括号时,您都会在堆栈上打开一个新框架(就像调用一个方法一样),并且当这个框架完成执行时 - 该框架将从堆栈中删除,包括在那里定义的所有本地参数。

改变:

if (Source == 1)
{
    String[] arith = {"Add","Sub"};
    codePos = Arrays.binarySearch(arith, 0, arith.length, Target);
}

至:

String[] arith = {"Add","Sub"};
if (Source == 1)
{                 
    codePos = Arrays.binarySearch(arith, 0, arith.length, Target);
}

应该可以解决您的问题。

于 2013-07-12T18:29:15.570 回答