gpt4 book ai didi

java - 每当用户输入负数时,我需要帮助尝试重新输入值

转载 作者:行者123 更新时间:2023-12-02 00:37:45 26 4
gpt4 key购买 nike

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 语句,但没有成功。谁能帮我?我有一个使用“标志”来控制循环的想法,但我不知道将其放在代码中的何处。我很确定有办法重新输入,但我就是不明白。

最佳答案

你的方法被破坏了 - 他们试图在值为负数时循环,但如果它是负数,他们会抛出异常......所以他们只会经历一次循环,要么返回一个值,要么抛出异常。

你想要这样的东西:

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来检查不同的类型是可怕的;只需捕捉您感兴趣的类型即可。

关于java - 每当用户输入负数时,我需要帮助尝试重新输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7270691/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com