Как мне повторить последний вопрос, если на него ответили неправильно? - PullRequest
2 голосов
/ 23 января 2020

Я знаю, это звучит немного странно, но я не знаю, как это выразить. Например, у меня есть сообщение об ошибке, которое появляется, когда пользователь вводит число больше 2000000. Однако после этого предполагается повторить вопрос «введите зарплату». Если они ответят правильно, программа спросит, хочу ли я ввести другого сотрудника. (В основном l oop перезапускается снова). Если они вводят неправильный ответ, программа снова повторяет то же сообщение об ошибке и предполагает, что пользователь снова может вводить данные, пока они не дадут правильный ответ.


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();

    }
}

Имеет ли смысл мой вопрос?

Ответы [ 2 ]

2 голосов
/ 23 января 2020

Я внес изменения в код. Изменения между комментариями // Changes start here и // Changes end here. Я добавил некоторое время l oop, чтобы продолжать проверять зарплату и просить пользователя ввести ее снова, если она превышает 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();

    }
}
0 голосов
/ 24 января 2020

Привет, ребята, спасибо за помощь, но я думаю, что я понял это. Я просто набрал код зарплаты сотрудника в то время как l oop.

while(empsal > 2000000) {
            System.out.println();
            System.out.println("Invalid salary entered! Please try again.");

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



            System.out.println();
        }
...