作为初学者,请多多包涵,
代码是摘录:
'CalcCost calculate = new CalcCost(); '
String key = stockNofive.getText();
String quantityTxt = quantityfive.getText();
int QuantityInt = Integer.parseInt(quantityTxt);
calculate.String(key, quantityTxt); // sending key and quantity to another class for user input check
calculate.Jlabel(stockJLBfive, quantityJLBfive); // sending jlabel for colour change if any error found
error.Colour();// recieve error value if any, if there is do not carry on,
//if error passes then do the following
calculate.setKey(key);
calculate.setQuantity(QuantityInt);
double cost = calculate.calculateBill();
information.append("\n\nTotal cost: £" + pounds.format(cost));
............
........
这是错误检查类
try {
if (key.equals("")) {
StockJLb.setText("<html><span style=\"color: red;\">Enter stock number</span></html>");
JOptionPane.showMessageDialog(null, "One or more fields missing");
return StockJLb;
}
if (QuantityStr.equals("")) {
QuantityJLb.setText("<html><span style=\"color: red;\">Enter quantity</span></html>");
JOptionPane.showMessageDialog(null, "One or more fields blank");
return QuantityJLb;
}
} catch (NumberFormatException nfe) {
QuantityJLb.setText("<html><span style=\"color: red;\">Enter quantity</span></html>");
JOptionPane.showMessageDialog(null, "'" + QuantityStr + "' is not a number");
error= true;
return QuantityJLb;
}
如果需要更多代码或其他任何东西,请回复,现在已经被困了 2 周。对不起,如果仍然不清楚我在代码中添加注释以提供帮助,我想要编码的是如果有错误不进行计算,如果没有并且它通过错误检查进行计算,我尝试了 if statments,并尝试添加 boolean ,但仍然无法弄清楚,