java,“如果通过则执行此操作然后执行此操作”

时间:2013-03-12 14:33:24

标签: java netbeans

请作为初学者加入我,

代码是     提取:

'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周。 我很抱歉,如果仍然不清楚我在代码中提供帮助, 我想要编码的是如果有错误不继续进行计算,如果没有并且它通过错误检查做计算,香港专业教育学院尝试过,如果我们尝试添加布尔值,但仍然无法弄明白,

3 个答案:

答案 0 :(得分: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
}

答案 1 :(得分: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));
}

答案 2 :(得分:0)

由于它只是代码的一小部分,因此根据条件在calculate.setKey(key);之后根据我的理解,您想要执行完整的图片并不困难。我是对的吗?

您始终可以将error.Colour()的值存储到某个变量中,即不确定它返回的内容,我假设它的类型为String。

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

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

//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包装代码。

希望这有帮助。