import java.util.*;
public class Inheritance {
public static Scanner objScan = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Enter name:");
String strName = objScan.nextLine();
double dSalary;
int iYears;
String strTIN;
String strLoopControl = "1";
try {
System.out.println("Enter salary:");
dSalary = dGetSalary();
System.out.println("Enter years worked:");
iYears = iGetYears();
System.out.println("Enter ID number:");
strTIN = strGetID();
Employee1 objEmp = new Employee1(strName, dSalary, iYears, strTIN);
objEmp.print();
}
catch (Exception e) {
if (e instanceof NegativeInputException) {
strLoopControl = "0";
System.out.println(e.getMessage());
//strLoopControl = "0";
}
else if (e instanceof ZeroIdentificationException) {
System.out.println(e.getMessage());
}
}
}
public static double dGetSalary () throws NegativeInputException {
double dSalary;
do {
try {
dSalary = objScan.nextDouble();
if (dSalary < 0) {
throw new NegativeInputException();
}
return dSalary;
}
catch (NegativeInputException nie) {
throw nie;
}
} while (dSalary<0);
}
public static int iGetYears() throws NegativeInputException {
int iYears;
try {
iYears = objScan.nextInt();
if (iYears < 0) {
throw new NegativeInputException();
}
return iYears;
}
catch (NegativeInputException nie) {
throw nie;
}
}
public static String strGetID() throws ZeroIdentificationException {
String strTIN;
try {
strTIN = objScan.next();
if (strTIN.equals("0000000")) {
throw new ZeroIdentificationException();
}
return strTIN;
}
catch (ZeroIdentificationException zie) {
throw zie;
}
}
}
class NegativeInputException extends Exception {
public NegativeInputException() {
super("You have inputted a negative number.");
}
public NegativeInputException (String strMessage) {
super(strMessage);
}
}
class ZeroIdentificationException extends Exception {
public ZeroIdentificationException() {
super("You inputted an invalid ID number. \n Please input again.");
}
public ZeroIdentificationException(String strMessage) {
super(strMessage);
}
}
class Person1 {
private String strName;
public Person1() {
strName = "";
}
public Person1(String strName) {
this.strName = strName;
}
public void setName(String strName) {
this.strName = strName;
}
public String getName() {
return strName;
}
public void print() {
System.out.println("Name: " +strName);
}
}
class Employee1 extends Person1 {
private String strName;
private double dSalary;
private int iYears;
private String strTIN;
public Employee1 (String strName, double dSalary, int iYears, String strTIN) {
this.strName = strName;
this.dSalary = dSalary;
this.iYears = iYears;
this.strTIN = strTIN;
}
public void print() {
System.out.println();
System.out.print("Name: " +this.strName);
System.out.println();
System.out.print("Salary:" +this.dSalary);
System.out.println();
System.out.print("Years WorkedS:" +this.iYears);
System.out.println();
System.out.print("ID Number:" +this.strTIN);
}
}
这是一个要求员工记录的程序。它要求提供姓名,工资,工作年限和身份证号码。每当用户输入负值时捕获异常,并在strTIN变量中输入“0000000”时捕获异常。这个程序完美无缺,但是每当输入负值和“0000000”时我需要重新输入。我尝试过使用do-while语句,但没有运气。谁能帮我?我有一个使用“flag”来控制循环的想法但我不知道在我的代码中把它放在哪里。我很确定有一种方法可以重新输入,我只是想不出来。
答案 0 :(得分:2)
你的方法被打破 - 他们试图在值为负时循环,但是如果它是否定的话它们会抛出异常......所以他们只会经历一次循环,要么返回一个值,要么抛出异常。
你想要这样的东西:
public static double getNonNegativeDouble(Scanner scanner) {
while (true) {
double value = scanner.nextDouble();
if (value >= 0) {
return value;
}
}
}
你应该可能重复提示并在无效输入上给出错误信息,但是......
另请注意:
抓住一个例外只是为了再次抛出它是可怕的:
// Ick: just don't catch! (May mean you don't need "try" block at all.)
catch (NegativeInputException nie) {
throw nie;
}
您的“伪匈牙利语”方法名称违反了Java命名约定
抓取Exception
然后使用instanceof
检查不同类型是可怕的;只是抓住你感兴趣的类型。
答案 1 :(得分:1)
您可以将NegativeInputException
代码包装在仅返回正值的while循环中,而不是抛出nextInt
...
此外,将输入功能包装在
等方法中也许是个好主意int readInteger(int minValue, int maxValue) {
if (minValue >= maxValue)
throw new IllegalArgumentException("...")
while(true) {
System.out.println("Please enter an integer ("+minValue+"-"+maxValue+")");
int val = objScan.nextInt();
if (val >= minValue && val <= maxValue) return val;
}
}
double readDouble(...