每当用户输入负数时,我需要帮助尝试重新输入值 import java.util.*; 公共类继承{ 公共静态扫描仪objScan=新扫描仪(System.in); 公共静态void main(字符串[]args){ System.out.println(“输入名称:”); 字符串strName=objScan.nextLine(); 双双数据表; 内耳; 字符串strTIN; 字符串strLoopControl=“1”; 试一试{ System.out.println(“输入薪资:”); dSalary=dGetSalary(); System.out.println(“输入工作年限:”); iYears=iGetYears(); System.out.println(“输入ID号:”); strTIN=strGetID(); Employee1 objEmp=新员工1(strName、dSalary、iyear、strTIN); print(); } 捕获(例外e){ if(负输入异常的实例){ strLoopControl=“0”; System.out.println(e.getMessage()); //strLoopControl=“0”; } else if(例如ZeroIdentificationException的实例){ System.out.println(e.getMessage()); } } } 公共静态双dGetSalary()引发NegativeInputException{ 双双数据表; 做{ 试试{ dSalary=objScan.nextDouble(); 如果(数据量=最大值) 抛出新的IllegalArgumentException(“…”) while(true){ System.out.println(“请输入一个整数(“+minValue+”-“+maxValue+”)); int val=objScan.nextInt(); 如果(val>=minValue&&val

每当用户输入负数时,我需要帮助尝试重新输入值 import java.util.*; 公共类继承{ 公共静态扫描仪objScan=新扫描仪(System.in); 公共静态void main(字符串[]args){ System.out.println(“输入名称:”); 字符串strName=objScan.nextLine(); 双双数据表; 内耳; 字符串strTIN; 字符串strLoopControl=“1”; 试一试{ System.out.println(“输入薪资:”); dSalary=dGetSalary(); System.out.println(“输入工作年限:”); iYears=iGetYears(); System.out.println(“输入ID号:”); strTIN=strGetID(); Employee1 objEmp=新员工1(strName、dSalary、iyear、strTIN); print(); } 捕获(例外e){ if(负输入异常的实例){ strLoopControl=“0”; System.out.println(e.getMessage()); //strLoopControl=“0”; } else if(例如ZeroIdentificationException的实例){ System.out.println(e.getMessage()); } } } 公共静态双dGetSalary()引发NegativeInputException{ 双双数据表; 做{ 试试{ dSalary=objScan.nextDouble(); 如果(数据量=最大值) 抛出新的IllegalArgumentException(“…”) while(true){ System.out.println(“请输入一个整数(“+minValue+”-“+maxValue+”)); int val=objScan.nextInt(); 如果(val>=minValue&&val,java,exception,input,Java,Exception,Input,而不是抛出NegativeInputException,您可以将nextInt代码包装在while循环中,该循环只返回正值 此外,也许将输入功能包装到如下方法中是个好主意 import java.util.*; public class Inheritance { public static Scanner objScan = new Scanner(System.in); public static void main(String[] args) {

而不是抛出
NegativeInputException
,您可以将
nextInt
代码包装在while循环中,该循环只返回正值

此外,也许将输入功能包装到如下方法中是个好主意

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

        }
int readInteger(int minValue,int maxValue){
如果(最小值>=最大值)
抛出新的IllegalArgumentException(“…”)
while(true){
System.out.println(“请输入一个整数(“+minValue+”-“+maxValue+”));
int val=objScan.nextInt();

如果(val>=minValue&&val而不是抛出一个
NegativeInputException
,您可以将
nextInt
代码包装在一个while循环中,该循环只返回正值

此外,也许将输入功能包装到如下方法中是个好主意

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

        }
int readInteger(int minValue,int maxValue){
如果(最小值>=最大值)
抛出新的IllegalArgumentException(“…”)
while(true){
System.out.println(“请输入一个整数(“+minValue+”-“+maxValue+”));
int val=objScan.nextInt();

如果(val>=minValue&&val您的方法被破坏-当值为负值时,它们尝试循环,但如果值为负值,它们将抛出异常…因此它们只会执行一次循环,返回值或抛出异常

你想要这样的东西:

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(...
您可能应该重复提示,并在无效输入时给出错误消息,尽管

还请注意:

  • 捕获一个异常,然后像这样再次抛出它是很可怕的:

    public static double getNonNegativeDouble(Scanner scanner) {
      while (true) {
        double value = scanner.nextDouble();
        if (value >= 0) {
          return value;
        }
      }
    }
    
  • 您的“伪匈牙利”方法名称违反Java命名约定

  • 捕捉
    Exception
    然后使用
    instanceof
    检查不同的类型是可怕的;只需捕捉您感兴趣的类型即可


您的方法已损坏-它们试图在值为负值时循环,但如果值为负值,它们将引发异常…因此它们只会执行一次循环,返回值或引发异常

你想要这样的东西:

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(...
您可能应该重复提示,并在无效输入时给出错误消息,尽管

还请注意:

  • 捕获一个异常,然后像这样再次抛出它是很可怕的:

    public static double getNonNegativeDouble(Scanner scanner) {
      while (true) {
        double value = scanner.nextDouble();
        if (value >= 0) {
          return value;
        }
      }
    }
    
  • 您的“伪匈牙利”方法名称违反Java命名约定

  • 捕捉
    Exception
    然后使用
    instanceof
    检查不同的类型是可怕的;只需捕捉您感兴趣的类型即可


我知道我在这里有点冒险,但我在尝试提问时添加了“家庭作业”标签,因为这样的代码在现实生活中是不可能使用的。非常感谢。这是我第一次在这里提问,所以我没有意识到标签的重要性。:)我知道我在这里有点冒险,但我在尝试提问时添加了“家庭作业”标签,因为这样的代码在现实生活中是不可能使用的。非常感谢。这是我第一次在这里提问,所以我没有意识到标签的重要性。:)我想在这里补充一点,使用异常来处理逻辑流——特别是在同一个方法中——是一个坏主意,而且很快就会变得混乱。我想这是一个合理的例子来解释为什么。根本不需要在这里抛出异常——你可以按照Jon的建议,将if块的结构更改为相同、更简单的结尾。我d在这里补充说,使用异常来处理逻辑流——特别是在同一个方法中——是一个坏主意,而且很快就会变得混乱。我想这是一个合理的例子,说明了为什么。根本不需要在这里抛出异常——正如Jon所建议的那样,您可以将if块的结构更改为相同、更简单的结尾。