简体   繁体   English

如何计算消耗的杯子量并将消耗的总水量存储在阵列中?

[英]How do I count the amount of cups consumed and store the total water amount consumed in an array?

I am receiving a string, which I convert to an integer that contains the water level in mL. 我收到一个字符串,我将其转换为包含以mL为单位的水位的整数。 I am displaying the water level in a textbox, and showing it's progress in a progress bar. 我在文本框中显示水位,并在进度栏中显示其进度。 I need to count the amount of cups consumed in cupcount, each cup being 400ml, and store the amount of water consumed in cup_amount. 我需要计算以cupcount为单位的杯子消耗量,每个杯子为400毫升,并以cup_amount存储消耗的水量。

String text = new String(txValue, "UTF-8");
                            /* listAdapter.add(text);
                             messageListView.smoothScrollToPosition(listAdapter.getCount()-1); */
                             String[] parts = text.split(",");
                             String part1 = parts[0];
                             String part2 = parts[1];
                             temperature.setText("Temperature: " + part1 + " °F");
                             capacitance.setText("Water Level: " + part2 + " mL");

                                     int number = 0;
                                     try{
                                         number= Integer.parseInt(part2);
                                     }catch(NumberFormatException nfe){

                                     }



                                    progressBar.setProgress(number);


                                int array[] = new int[200];
                               int cup_amount=0;
                             for(int a =0;a<array.length;a++) {
                                     array[a] = number;

                                     if(array[a]>0)
                                         cup_amount = array[a]-array[a-1];
                                        if(cup_amount >= 400)
                                        {
                                            cupcount= cupcount + 1;
                                            cup_amount=0;

                                        }
                                        Log.d("Array","arr: "+ Arrays.toString(array));

                                  }

                             cup_count.setText("Water intake = " + cupcount + " cups");

                         } catch (Exception e) {
                             Log.e(TAG, e.toString());
                         }

                     }

The problem is cup_count is not updating when the water level changes by at least 400mL. 问题是,当水位变化至少400mL时,cup_count不会更新。 How can I fix this? 我怎样才能解决这个问题?

EDIT: Sorry for the confusion, my expected input is the integer, 'number', that updates as the water level increases, it will be somewhere from 0mL to 500mL. 编辑:对不起,我的困惑是我的期望输入是整数'number',该整数随着水位的增加而更新,它将介于0mL至500mL之间。

I am trying to record the amount of cups of water consumed, which will occur if the user drinks 400mL or more, and store it in the variable 'cup_count' so I can display it to the EditText box 'cupcount' in XML. 我正在尝试记录消耗的杯水量,如果用户喝400mL或更多,则会发生这种情况,并将其存储在变量“ cup_count”中,以便可以将其显示在XML的EditText框“ cupcount”中。 I also want to record the total amount of water consumed in mL so I can inform the user that they have not had enough water in a certain time period, this will be stored in the variable 'cup_amount'. 我还想记录以mL为单位的水消耗总量,因此我可以通知用户在一定时间内他们没有足够的水,这将存储在变量“ cup_amount”中。 I'm not even sure if using an array is the best way to store these values. 我什至不确定使用数组是否是存储这些值的最佳方法。

EDIT: 编辑:

I'm not sure if this is what you try achieve, but here you go: 我不确定这是否是您尝试实现的目标,但是您可以执行以下操作:

public class MyClass {
    public static void main(String args[]) {
        int total_amount = 2750;
        int cup_count = total_amount / 400;
        int remainder = total_amount % 400;

        System.out.println("Water intake = " + cup_count + " cups.\nRemainder = " + remainder + " ml.");
    }
}

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

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