2014-09-21 64 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