简体   繁体   English

JAVA中多个选中复选框的总和

[英]Sum of multiple selected checkboxes in JAVA

If I use the "+ " sign it will just concatenate the values, but I need to find the mathematical values.如果我使用“+”符号,它只会连接值,但我需要找到数学值。 I need to insert the quantity of a product.我需要插入产品的数量。 How to find the sum when multiple checkboxes are selected?选择多个复选框时如何找到总和? I have tried this which concatenates.. Also the values need to go into the database, it is working with the concatenated values..我试过这个连接..此外,这些值需要进入数据库,它正在处理连接的值..

                 if(chckbx1.isSelected()){
                    qty= chckbx1.getText();
                    chckbx1.setSelected(true);
             }

             if(chckbx1.isSelected() && chckbx5.isSelected()){
                    qty= chckbx1.getText() + chckbx5.getText();
                    chckbx5.setSelected(true);
                    chckbx1.setSelected(true);
             }

您需要使用 Double.parseDouble() 将其解析为 Double

qty= (Double.parseDouble(chckbx1.getText()) + Double.parseDouble(chckbx5.getText())) + "";

您可以使用 Integer.parseInt() 将其解析为 Integer

qty= (Integer.parseInt(chckbx1.getText()) + Integer.parseInt(chckbx5.getText())) + "";

Yes, Using the + operator on Strings concatenates them.是的,在字符串上使用 + 运算符将它们连接起来。

So, you need to convert the Strings to numbers.因此,您需要将字符串转换为数字。
Depending upon what type of numbers the Strings contain (real or integer), you may parse them to numbers in one of the following ways:根据字符串包含的数字类型(实数或整数),您可以通过以下方式之一将它们解析为数字:

  • qty= ""+ ( Integer.parseInt(chckbx1.getText()) + Integer.parseInt(chckbx5.getText()) );
  • qty= ""+ ( Double.parseDouble(chckbx1.getText()) + Double.parseDouble(chckbx5.getText()) );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM