我在按钮中添加代码时出现了一些问题,例如:我在数量上键入2将*价格和数量示例400 * 2 = 800但是当我再次键入2表示800 x 2 = 1600时,任何人都可以引导我?谢谢错误显示在最后2行。
`private void jButton2ActionPerformed(java.awt.event.ActionEvent evt){
int id = Integer.parseInt(jTextField1.getText());
int qty = Integer.parseInt(jTextField2.getText());
purchasecontroller.PurchaseProduct(id, qty);
String getname = displaycontroller.SearchbyProductName(id);
jLabel4.setText( "" + getname );
jLabel3.setText("" + qty);
jList1.addElement(getname + qty);
// jList1.add(new Product("Hello", 1));
String getprice = displaycontroller.SearchbyProductPrice(id);
int total = qty * Integer.parseInt (getprice);
jLabel11.setText("" + total );
int finals = (total * qty);
jLabel12.setText("" + finals );
}
`
答案 0 :(得分:1)
应该是
int total = qty * Integer.parseInt (getprice);
而不是
int total = qty * getprice;
答案 1 :(得分:1)
在这一行:
int total = qty * getprice;
你试图将int(qty)乘以String(getprice),这是无法完成的。
您需要将getprice解析为整数,然后在total
赋值中为getprice替换该新整数。