2013-07-12 21 views
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; 
       } 

      } 
     } 

    } 

} 
+0

當你運行這個程序時會發生什麼? –

+0

問題是什麼? –

+2

看起來你已經準備好回答你自己的問題了... – jahroy

回答

2

你的問題是一個作用域的問題:你定義arith內的「如果」,然後您嘗試使用它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); 
} 

應該解決您的問題。

+0

感謝您的mail.i糾正了上述問題。糾正後,在執行代碼的同時,我在線程「main」中得到了以下錯誤異常java.lang.ClassCastException:java.lang.Integer不能轉換爲java.lang.String \t在java.lang.String.compareTo(未知來源) \t在java.util.Arrays.binarySearch0(未知來源) \t在java.util.Arrays.binarySearch(未知來源) \t在Choice1.main(選擇1的.java:25)。 –

+0

問題解決了... –