java - 当最后一个问题被回答错误时,我该如何重复?
问题描述
我知道这听起来有点奇怪,但我不知道该怎么说。例如,当用户输入大于 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();
}
}
推荐阅读
- javascript - 基于出现次数的数组排序
- python - 在 ubuntu 和 linux 上使用 pyinstaller 创建可执行文件时出现图标(快捷方式)问题
- php - PHP 致命错误:未捕获错误:调用未定义函数 get_magic_quotes_gpc()
- c# - 在 MVVM 中创建静态视图模型是正确的方法吗?
- css - 防止旋转木马内的组件调整大小
- react-native - base64 的图像文件路径在 iOS 上不起作用(React-Native)
- postgresql - 锁定表或行以检查条件并插入行
- php - 按信息订购尚未批准或拒绝
- python - 如何使用 python3 将 wiki-link 图像转换为普通的 md-link 图像?
- jquery - 我们如何在一个地方定义默认配置值并将其用于应用程序中的所有数据表