Java 如果在不使用循环的情况下满足条件,则调用方法重新启动

Java 如果在不使用循环的情况下满足条件,则调用方法重新启动,java,user-interface,oop,jtextfield,jtextarea,Java,User Interface,Oop,Jtextfield,Jtextarea,我正在为学校的工资项目工作。我使用GUI才两周,所以我的技能有很大的发展空间。除了这个方法,一切都很好。如果输入的数字超出范围,我试图获得清除JTextArea的方法,并跳过将数据发送到JTextArea的剩余代码。现在,如果超出范围,它将打印名称、部门和0.00,但我不希望它显示任何内容,除非它在范围内 public void buildAgain() { // declare variables to hold the JTextField input as string

我正在为学校的工资项目工作。我使用GUI才两周,所以我的技能有很大的发展空间。除了这个方法,一切都很好。如果输入的数字超出范围,我试图获得清除JTextArea的方法,并跳过将数据发送到JTextArea的剩余代码。现在,如果超出范围,它将打印名称、部门和0.00,但我不希望它显示任何内容,除非它在范围内

public void buildAgain() {

    // declare variables to hold the JTextField input as string
    String rateStr, hoursStr, firstName, lastName, dept;


    firstName = (String) fnameField.getText();
    lastName = (String) lnameField.getText();

    rateStr = rateField.getText();
    // convert the rate field into double in order to calculate it
    rate = Double.parseDouble(rateStr);


    hoursStr = hoursField.getText();
    // convert the hours into double in order to calculate it
    hoursWorked = Double.parseDouble(hoursStr);

    // Check how many hours worked to make sure they are within range
    if(hoursWorked >= 0 && hoursWorked <=60)
        hours = hoursWorked;

    // Display message if hours are not within range
    else
        JOptionPane.showMessageDialog(null, "You have entered an invalid number of hours. \n"
                          + " Please enter a number between 0 and 60.", "Hour Entry Error", 
                          JOptionPane.ERROR_MESSAGE);

    // Clears JTextFields after the error message
    fnameField.setText("");
    lnameField.setText("");
    hoursField.setText("");
    rateField.setText("");

    // check the hourly rate to make sure it is within  range
    if (rate >= 6 && rate <=150)
        payRate = rate;

    // display an error message if rate entered not within range
    else 
        JOptionPane.showMessageDialog(null, "You have entered an invalid pay rate. \n "
                          + "Please enter the rate between 6 and 150.", "Pay Rate Error", 
                           JOptionPane.ERROR_MESSAGE);

    // clear the JTextFields  after error message
    fnameField.setText("");
    lnameField.setText("");
    hoursField.setText("");
    rateField.setText("");

    // calculate the pay regular hours
    if (hours >= 0 && hours <= 40){
        weeklyPay = hours*payRate;

    // Calculate overtime pay
    } else if (hours > 40 && hours <= 60){
        overTime = (hours-40) * (payRate*1.5);
        weeklyPay = overTime + (40*payRate);
    }

    // Display the total pay in uneditable table
    totalPayField.setText("$"+dollar.format(weeklyPay));

    // Send name to JTextArea
    list.append(firstName + " ");
    list.append(lastName);

    // Get the selected department
    if(hr.isSelected())
        dept = hr.getText();
    else if(accounting.isSelected())
        dept = accounting.getText();//"Accounting";
    else if(marketing.isSelected())
        dept = marketing.getText();
    else if(sales.isSelected())
        dept = sales.getText();
    else
        dept = shipping.getText();

    // Send selected department and pay to JTextArea
    list.append("\t" + dept);
    list.append("\t$" + dollar.format(weeklyPay) + "\n");

    }
public void buildreach(){
//声明变量以将JTextField输入保存为字符串
字符串rateStr、hourstr、firstName、lastName、dept;
firstName=(字符串)fnameField.getText();
lastName=(字符串)lnameField.getText();
rateStr=rateField.getText();
//将rate字段转换为double以计算它
rate=Double.parseDouble(rateStr);
hoursStr=hoursField.getText();
//将小时数转换为双倍,以便进行计算
hoursWorked=Double.parseDouble(hoursStr);
//检查工作小时数,确保其在工作范围内
如果(工作小时数>=0&&hoursWorked=6&&rate=0&&hours40&&hours你就快到了

将清除JTextFields的代码段移到检查工作时间在范围内的代码段之前。然后在显示错误消息后返回。这样,方法的执行不会继续

//Clears JTextFields before checking the error message
fnameField.setText("");
...

//check how many hours worked to make sure they are within range
if(hoursWorked >= 0 && hoursWorked <=60)
    hours = hoursWorked;

//Display message if hours are not within range
else
    JOptionPane.showMessageDialog(null, "error msg.\n", JOptionPane.ERROR_MESSAGE);
    return
//在检查错误消息之前清除JTextFields
fnameField.setText(“”);
...
//检查工作小时数,确保其在工作范围内

如果(hoursWorked>=0&&hoursWorked=0&&hoursWorked=0&&hoursWorked非常感谢兄弟。如此简单,但它离我如此遥远,因为我不知道。我希望我能投票支持它。再次感谢。
//check how many hours worked to make sure they are within range
if(hoursWorked >= 0 && hoursWorked <=60) {
    hours = hoursWorked;

//Display message if hours are not within range
} else {
    JOptionPane.showMessageDialog(null, "error msg.\n", JOptionPane.ERROR_MESSAGE);
    //Clears JTextFields before checking the error message
    fnameField.setText("");
    ...
    return
}