java - 当 try-catch block 后面有代码时,Try-Catch 不会重新运行

标签 java netbeans

我试图在 do-while 循环中运行多个 try-catch block ,以帮助捕获错误,例如整数和 double 输入的 InputMissMatch,但是,我仍然需要能够在同一个 do-while 中输入字符串从控制台界面循环。

是否可以让 try-catch block 在捕获异常后重新运行,而不是移至下一行代码,如果可以,如何实现?

这是我的代码:

import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.Map;
import java.util.Scanner;


public class EmployeeBenefits {

    Map <Integer, CaptureDetails> map = new HashMap<> ();

    int number;
    String name;
    String surname;
    double grossSalary;

    int employeeNumber = 1;
    int numberOfEmployees = 0;

    public void captureDetails() {

        boolean codeIsRunning = true;

        Scanner input = new Scanner (System.in);

        do {

            inputNumberOfEmployees ();

            if (numberOfEmployees != 0) {

                for (int enterDetails = 0; enterDetails < numberOfEmployees; enterDetails++) {

                    inputEmployeeNumber ();
                    inputEmployeeNames ();
                    inputGrossSalary ();

                    map.put(employeeNumber, new CaptureDetails (number, name, surname, grossSalary));
                    employeeNumber++;
                }
            }

            codeIsRunning = false;

        } while (codeIsRunning); // end of do-while loop

    } //  end of captureDetails () method

    public void inputNumberOfEmployees () {

        Scanner input = new Scanner (System.in);

        try {

            System.out.print ("Enter the number of employees to input: ");
            numberOfEmployees = input.nextInt();

        }
        catch (InputMismatchException e) {

            System.out.println ("\nYou must enter an integer value. Please try agin\n");
            input.nextLine();
        }

    } // end of inputNumberOfEmployees()

    public void inputEmployeeNumber () {

        Scanner input = new Scanner (System.in);

        try {

            System.out.print ("Enter Number: ");
            number = input.nextInt();

        } catch (InputMismatchException e) {

            System.out.println("\nYou must enter an integer value. Please try agin\n");
            input.nextLine ();

        }

    } // end of inputEmployeeNumber()

    public void inputGrossSalary () {

        Scanner input = new Scanner (System.in);

        try {
            System.out.print ("Enter Gross Salary: ");
            grossSalary = input.nextDouble();
        }
        catch (InputMismatchException e) {
            System.out.println ("\nEnter employees salary. Please try agin\n");
            input.nextLine ();
        }

    } // inputGrossSalary ()

    public void inputEmployeeNames () {

        Scanner input = new Scanner (System.in);

        System.out.print ("Enter Name: ");
        name = input.nextLine();

        System.out.print ("Enter Surname: ");
        surname = input.nextLine();

    } // end of inputEmployeeNames

    public static void main(String[] args) {

        EmployeeBenefits eb = new EmployeeBenefits ();

        boolean programIsRunning = true;

        Scanner input = new Scanner (System.in);

        while (programIsRunning) {

            System.out.println ("1. Enter Employee details");
            System.out.println ("2. Set New Salary");
            System.out.println ("3. Print Employee Details");
            System.out.println ("4. Exit");

            switch (input.nextInt()) {

                case 1:
                    eb.captureDetails ();
                    break;
                case 2:
                    break;
                case 3:
                    break;
                case 4:
                    programIsRunning = false;
                    break;

                default :
                    System.out.println ("Enter a value between from 1 to 4\n");

            } // end of switch-case (input.nextInt())

        } // end of while (programIsRunning) loop  

    } // end of main method

} // end of EmployeeBenefits class


class CaptureDetails {

    int number;
    String name, surname;
    double grossSalary;

    public CaptureDetails (int number, String name, String surname, double grossSalary) {

        this.number = number;
        this.name = name;
        this.surname = surname;
        this.grossSalary = grossSalary;

    }

} // end of CaptureDetails class

最佳答案

您当然可以循环提示输入。问题是......你没有使用循环:

public void inputNumberOfEmployees () {
    Scanner input = new Scanner (System.in);
    try {
        System.out.print ("Enter the number of employees to input: ");
        numberOfEmployees = input.nextInt();
    }
    catch (InputMismatchException e) {
        System.out.println ("\nYou must enter an integer value. Please try agin\n");
        input.nextLine();
    }
}

如果您想在循环中执行此逻辑,请将其包装在循环中。在这种情况下,终止条件是输入是否成功。所以你可以用一个简单的 boolean 标志来跟踪它。也许是这样的:

public void inputNumberOfEmployees () {
    Scanner input = new Scanner (System.in);

    boolean isInvalid = true;
    while (isInvalid) {

        try {
            System.out.print ("Enter the number of employees to input: ");
            numberOfEmployees = input.nextInt();

            isInvalid = false;
        }
        catch (InputMismatchException e) {
            System.out.println ("\nYou must enter an integer value.\n");
        }

    }
}

关于java - 当 try-catch block 后面有代码时,Try-Catch 不会重新运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46369078/

相关文章:

php - Netbeans 7 在哪里存储 FTP 密码

netbeans - 'copyfiles' 是标准的 Ant 任务吗?

java - 如何将内容写入自定义 java Properties 文件

java - 一个带开关盒的简单计算器

java - 这是嵌套类的一个很好的用途吗?

java - 无法添加 CSS 文件

Java Swing : Add a component by code in NetBeans

java - 删除两个以上的连续出现

java - 编写密码、PIN 和 PUK 程序

java - Ubuntu 上的 File.getAbsolutePath 不正确