2014-09-21 63 views
0

我想知道如何從我的switch語句中得到結果到新的變量中。將語句輸出數據切換到新的變量

這是我的代碼,一旦switch語句找到正確的員工,我希望將這些信息轉到新變量。我將如何做到這一點?

此外,如果我在僱員號字段中鍵入一個字符或字符串,我怎麼會得到它返回和錯誤而不是崩潰的應用程序?

package payRoll; // package Name 

/////////////////////////////// 

import java.util.ArrayList; 
import java.util.Scanner; 

//////////////////// API Imports 

public class Main { 

    public static void main(String[] args) { 
    Scanner keyboard = new Scanner (System.in); 

    /////////// Code 

    String cacheEm = new String(); 

    ArrayList<String[]> addresses = new ArrayList<String[]>(); 

    String[] EmNo = new String[4]; { 
     EmNo[0] = "Shaun Clark"; 
     EmNo[1] = "Ann Clark"; 
     EmNo[2] = "Darren Watters"; 
     EmNo[3] = "Daniel Brightman"; 

    addresses.add(EmNo); 

    }  
    boolean repeat; 
    do { 
     repeat = false; 

    System.out.print("Please Enter Employee number: ");  
    int employeeNum = keyboard.nextInt(); 
    switch (employeeNum) 
    {  
    case 1: employeeNum = 0; 
    System.out.println("Employee Indexed as " + EmNo[0]); 
    break; 
    case 2: employeeNum = 1; 
    System.out.println("Employee Indexed as " + EmNo[1]); 
    break; 
    case 3: employeeNum = 2; 
    System.out.println("Employee Indexed as " + EmNo[2]); 
    break; 
    case 4: employeeNum = 3; 
    System.out.println("Employee Indexed as " + EmNo[3]); 
    break; 
    default:    
     System.err.println("\n Employee Not found!! \n"); 
     repeat = true;   
     } 
    }  
    while(repeat);  

    keyboard.close(); 



    ////// output from switch needs to go into new variable for next function 


    }// end class 
}// end main 

回答

2

您已經有了結果employeeNum,只是檢查它小於4,你是好。只要得到EmNo[employeeNum],你應該做的,而不是那個switch語句......

爲了捕捉輸入錯誤,你應該捕捉異常與try-catch語句拋出。

0

林想知道如何從我的switch語句得到的結果到一個新的變量

您必須聲明do-while循環的employeeNum變量之外你能後使用它循環。

你可以把它的頂部,你聲明變量:

// ... 
String cacheEm = new String(); 
int employeeNum; 
// ... 

然後你可以使用它在循環中:

// ... 
boolean repeat; 
do { 
    repeat = false; 

System.out.print("Please Enter Employee number: ");  
employeeNum = keyboard.nextInt(); // notice no 'int' because it is already declared at the top of the program 
// ... rest of the code 

如果我輸入一個字符或字符串中員工編號字段,我將如何得到它返回和錯誤,而不是崩潰的應用程序?

有這樣做的幾種方法,一個方法是趕上了無效的輸入異常:

由於您使用的是Scanner.nextInt()方法,這個方法拋出InputMismatchException如果下一個標記不匹配Integer正則表達式,或者超出範圍意義太大的數字。所以你可以把你的代碼放在try/catch中,並捕獲這個異常並打印出錯誤。

// ... 
boolean repeat; 
do { 
    repeat = false; 
    System.out.print("Please Enter Employee number: "); 
    try { 
     employeeNum = keyboard.nextInt(); 
     switch (employeeNum) { 
      // ... rest of your code 
     } 
    } catch (InputMismatchException ime) { 
     System.err.println("Please enter an integer"); 
     // or you can just print the stack trace like ime.printStackTrace() which is pretty standard 
    } 
} while(repeat);  
keyboard.close(); 

// use employeeNum here 

另一種方法是使用Scanner.next()閱讀employeeNumString,然後嘗試解析它作爲一個int,趕上NumberFormatException如果值是不是整數:

boolean repeat; 
do { 
    repeat = false; 
    System.out.print("Please Enter Employee number: "); 
    try { 
     employeeNum = Integer.parseInt(keyboard.next()); 
     switch (employeeNum) { 
      // ... rest of your code 
     } 
    } catch (NumberFormatException nfe) { 
     System.err.println("Please enter an integer"); 
     // or you can just print the stack trace like nfe.printStackTrace() which is pretty standard 
    } 
} while(repeat);  
keyboard.close(); 

// use employeeNum here