如果不使用循环,则在满足条件时调用方法重新启动

calling a method to restart if a condition is met without using a loop

我正在为学校开发一个薪资项目。我只用了两个星期就开始使用 GUI,所以我的技能有很大的发展空间。除此方法外,一切正常。如果输入的数字超出范围,我试图获取清除 JTextAreas 的方法,并跳过将数据发送到 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");

    }

你快到了。

将清除 JTextFields 的代码部分移到检查工作时间是否在范围内的代码部分之前。然后 return 就在您显示错误消息之后。这样方法的执行就不会继续。

//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

注意:如果您只想在出现错误后清除字段,请将它们移到显示错误消息的 else 语句中。

例如:

//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
}