2013-03-12 107 views
-5

请多多包涵作为我是一个初学者,java的,“这样做,如果通过,那么这样做”

代码 提取物:

'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周,请回复。 即时通讯如果仍然unlclear我把代码中的笔记帮助, 我想代码是如果有错误不进行计算,如果没有,它通过错误检查做的计算,ive试过如果statments,并尝试添加布尔值,但仍然无法算起来,

+0

也许你可以放些更多的代码来理解上下文以及你想做什么。 – 2013-03-12 14:35:13

+2

错误通过的含义是什么? – 2013-03-12 14:36:35

+0

你的问题是非常不清楚,从我看到我只是建议你阅读关于try/catch块:-) – Gatekeeper 2013-03-12 14:37:41

回答

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)); 
} 
+0

我试过,如果说法,但不知道的条件,使用 – user2158735 2013-03-12 15:02:09

+0

@ user2158735这就是为什么我之前问过,“这是什么意思的错误通过?” – 2013-03-12 15:02:50

+0

它意味着不携带直到用户输入一些有效的东西,这是你的吗? – user2158735 2013-03-12 15:16:20

0

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

(SO然后将它保存到一个变量或一个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 
} 
+0

'calculate.setKey(key); calculate.setQuantity(QuantityInt); double cost = calculate.calculateBill(); information.append(“\ n \ n总成本:£”+磅。格式(成本)); ............ ........'是成功 – user2158735 2013-03-12 14:54:32

+0

它返回一个Jlabel值,'valueOfError'会是什么? – user2158735 2013-03-12 15:32:41

+0

那么,它会返回一个JLabel组件或标签的文本?我的意思是标签的.getText()方法? valueError取决于你的期望。你的应用程序的要求是什么?什么时候应该是错误,什么时候应该成功? – 2013-03-13 15:50:47

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。

希望这会有所帮助。

+0

返回值是JLabel的颜色变化, – user2158735 2013-03-12 22:50:28