为了账号安全,请及时绑定邮箱和手机立即绑定

使用循环的众多字符串值的总和

使用循环的众多字符串值的总和

慕娘9325324 2023-10-12 14:40:40
在此处输入图像描述我有一个名为价格的字符串。我从 firebase 数据库中获取 Price 的值。我想将这些字符串值相加并得出总价。价格值根据数据库中的子项自动增加。例如,值为 100、70、50、20、40 等。它会相应地增加。我认为应该有一个循环来使所有这些值的总和成为总价。这就是我到目前为止所做的。 int pos = 0;                                      JSONObject jsonObject = new JSONObject(); if (jsonObject.has(Integer.toString(pos))) { } else {                                            try {                                                        jsonObject.put(Integer.toString(pos),Price);                                              } catch (JSONException e) {                                                    e.printStackTrace();                                              }                                     } for (int i = 0; i < jsonObj.length(); i++) {     try {                                           String itemInArray = jsonObj.getString(String.valueOf(i));         int Sum =  0;         int totalPrice = sum + itemInArray;  } catch (JSONException e) {                                    e.printStackTrace();                                }                            }但是代码不起作用。请提供任何帮助,我们将不胜感激。
查看完整描述

1 回答

?
冉冉说

TA贡献1877条经验 获得超1个赞

 int pos = 0;                                     

 JSONObject jsonObject = new JSONObject();

 if (jsonObject.has(Integer.toString(pos))) {

 } else {                                       

     try {                                               

         jsonObject.put(Integer.toString(pos),Price);                                         

     } catch (JSONException e) {                                           

         e.printStackTrace();                                         

     }                                    

 }


 for (int i = 0; i < jsonObj.length(); i++) {

     try {                                  

         String itemInArray = jsonObj.getString(String.valueOf(i));

         int Sum =  0;

         int totalPrice = sum + itemInArray;


  } catch (JSONException e) {

                                    e.printStackTrace();

                                }


                            }

但是代码不起作用。请提供任何帮助,我们将不胜感激。


查看完整回答
反对 回复 2023-10-12
  • 1 回答
  • 0 关注
  • 99 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信