将Statement语句输出数据转换为新的Var

时间:2014-09-21 00:07:57

标签: java

我想知道如何将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 个答案:

答案 0 :(得分:2)

你已经在employeeNum中得到了结果,只需检查它是否小于4就可以了。只要得到你应该做的EmNo[employeeNum]而不是那个switch语句......

为了捕获输入中的错误,您应该捕获使用try-catch语句抛出的异常。

答案 1 :(得分:0)

  

我想知道如何将我的switch语句的结果转换为新的变量

您必须在employeeNum循环之外声明do-while变量,才能在循环后使用它。

您可以将它放在声明变量的顶部:

// ...
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

另一种方法是使用employeeNumString作为Scanner.next()阅读,然后尝试将其解析为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