-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週間これに固執してください。それでも不明な場合は、コードにメモを入れてください。エラーが発生した場合は計算を続行しないでください。エラーがない場合はエラーチェックに合格し、計算を実行します。ステートメントの場合は試行します。ブール値を追加しようとしましたが、それでも理解できません。

4

3 に答える 3

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にラップする必要があります。

お役に立てれば。

于 2013-03-12T14:44:35.977 に答える
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 に答える