-5

作为初学者,请多多包涵,

代码是摘录:

'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 ,但仍然无法弄清楚,

4

3 回答 3

0

error.Colour();// return the error value

(然后将其保存到变量中或在 if 条件下使用它!)

if (error.Colour() == valueOfError) {
            calculate.setKey(key);
            calculate.setQuantity(QuantityInt);
            double cost = calculate.calculateBill();
            information.append("\n\nTotal cost: £" + pounds.format(cost));
            ............
            ........
} else {
//It is success
}
于 2013-03-12T14:37:25.877 回答
0

你不是在寻找一个if声明是吗?

if(error.Colour() == /*insert value to check here*/)
{
    calculate.setKey(key);
    calculate.setQuantity(QuantityInt);
    double cost = calculate.calculateBill();
    information.append("\n\nTotal cost: £" + pounds.format(cost));
}
于 2013-03-12T14:37:41.373 回答
0

由于它只是代码的一小部分,因此很难获得整个图片,但是无论我理解什么,您都想calculate.setKey(key);根据条件执行之后的行。我对么?

您始终可以将 的值存储error.Colour()到某个变量中,即不确定它返回什么,我假设它是字符串类型。

String colourErrorValue = error.Colour();// return the error value

然后您可以检查该值以确保它没有任何错误。IE

//if error passes then do the following
if(colourErrorValue.equals("NO ERROR VALUE") { // I don't know what is no error scenario in                              
                                             //your case but just assuming the string
        calculate.setKey(key);
        calculate.setQuantity(QuantityInt);
        double cost = calculate.calculateBill();
        information.append("\n\nTotal cost: £" + pounds.format(cost));
        ............
        ........
}

如果该error.Colour()方法抛出异常或错误,那么您只需将代码包装在 try 和 catch 周围。

希望这可以帮助。

于 2013-03-12T14:44:35.977 回答