java - 当最后一个问题回答错误时,如何重复?

标签 java java.util.scanner do-while

我知道这听起来有点奇怪,但我不知道该怎么表达。例如,当用户输入大于 2000000 的数字时,我会显示一条错误消息。但是,之后应该重复“输入工资”问题。如果他们输入正确的答案,程序就会询问我是否愿意输入另一名员工。 (基本上循环再次重新开始)。如果他们再次输入错误的答案,程序会重复相同的错误消息,并假设让用户再次输入,直到他们给出有效的答案。


import java.util.Scanner;
import java.text.*;
import java.io.*;

public class Project1 {

    public static void main(String[] args) throws FileNotFoundException

    {

        Scanner keyboard = new Scanner(System.in);
        Scanner user_input = new Scanner(System.in);
        Scanner scan = new Scanner(System.in);

        PrintWriter outFile = new PrintWriter("Project1.out");

        String employee_Fname;
        String employee_Lname;
        String employee_city;
        String employee_state;
        double empzip;
        String employee_job;
        double empsal;
        char again;
        int count = 1;
        String answer;

        do {

            System.out.print("Enter Employees First Name: ");
            employee_Fname = user_input.next();
            System.out.println();

            System.out.print("Enter the employee's last name: ");
            employee_Lname = user_input.next();
            System.out.println();

            System.out.print("Enter employee's city: ");
            employee_city = user_input.next();
            System.out.println();

            System.out.print("Enter employee's state: ");
            employee_state = user_input.next();
            employee_state.toUpperCase();
            System.out.println();

            System.out.print("Enter employee's zipcode: ");
            empzip = keyboard.nextDouble();
            System.out.println();

            System.out.print("Enter employee's job title: ");
            employee_job = user_input.next();
            System.out.println();

            System.out.print("Enter employee's salary: ");
            empsal = keyboard.nextDouble();
            System.out.println();

            if (empsal > 2000000) {
                System.out.println();
                System.out.println("Invalid salary entered! Please try again.");
                empsal = keyboard.nextDouble();
                System.out.println();

            } else

                System.out.print("Do you want to enter another employee? Y/N?");
            answer = keyboard.next();
        } while (answer.equals("Y"));

        outFile.printf("Employee first name is:  " + employee_Fname);
        outFile.printf("Employee last name is:  " + employee_Lname);
        outFile.printf("Employee city is:  " + employee_city);
        outFile.printf("Employee state is:  " + employee_state);
        outFile.printf("Employee zipcode is:  " + empzip);
        outFile.printf("Employee job is:  " + employee_job);
        outFile.printf("Employee salary is:  " + empsal);

        outFile.close();

    }
}

我的问题有意义吗?

最佳答案

我已经对代码进行了更改。更改位于注释 //Changes start here//Changes end here 之间。 我添加了一个 while 循环来不断检查工资,如果超过 2000000,则要求用户再次输入。


import java.util.Scanner;
import java.text.*;
import java.io.*;

public class Project1{

    public static void main(String[] args) throws FileNotFoundException

    {

        Scanner keyboard = new Scanner(System.in);
        Scanner user_input = new Scanner(System.in);
        Scanner scan = new Scanner(System.in);

        PrintWriter outFile = new PrintWriter("Project1.out");

        String employee_Fname;
        String employee_Lname;
        String employee_city;
        String employee_state;
        double empzip;
        String employee_job;
        double empsal;
        char again;
        int count = 1;
        String answer;

        do {

            System.out.print("Enter Employees First Name: ");
            employee_Fname = user_input.next();
            System.out.println();

            System.out.print("Enter the employee's last name: ");
            employee_Lname = user_input.next();
            System.out.println();

            System.out.print("Enter employee's city: ");
            employee_city = user_input.next();
            System.out.println();

            System.out.print("Enter employee's state: ");
            employee_state = user_input.next();
            employee_state.toUpperCase();
            System.out.println();

            System.out.print("Enter employee's zipcode: ");
            empzip = keyboard.nextDouble();
            System.out.println();

            System.out.print("Enter employee's job title: ");
            employee_job = user_input.next();
            System.out.println();

            System.out.print("Enter employee's salary: ");
            empsal = keyboard.nextDouble();
            System.out.println();

            // Changes start here
            while (empsal > 2000000) {
                System.out.println();
                System.out.println("Invalid salary entered! Please try again.");
                empsal = keyboard.nextDouble();
                System.out.println();
            }
            // Changes end here

            System.out.print("Do you want to enter another employee? Y/N?");
            answer = keyboard.next();

        } while (answer.equals("Y"));

        outFile.printf("Employee first name is:  " + employee_Fname);
        outFile.printf("Employee last name is:  " + employee_Lname);
        outFile.printf("Employee city is:  " + employee_city);
        outFile.printf("Employee state is:  " + employee_state);
        outFile.printf("Employee zipcode is:  " + empzip);
        outFile.printf("Employee job is:  " + employee_job);
        outFile.printf("Employee salary is:  " + empsal);

        outFile.close();

    }
}

关于java - 当最后一个问题回答错误时,如何重复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59872950/

相关文章:

c - c 中的 do-while 或 scanf() 不起作用

java - Number 类 doubleValue 的异常

java - 正则表达式示例困惑

代码不会在 C 中的 do while 循环中执行

java - 您将如何提高此正则表达式的效率

java - 获取线路的特定部分

c++ - 为用户提供循环 for 循环的选项

java - 什么是NullPointerException,我该如何解决?

java - 计算相对于没有夏令时的时区的正确时间

java - 接收带有空格的字符串