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».Я пытался использовать заявления типа «пока», но не повезло.Может кто-нибудь мне помочь?У меня есть идея использовать «флаг» для управления циклом, но я не знаю, где поместить его в моем коде.Я почти уверен, что есть способ повторного ввода, я просто не могу понять это.