[英]How do I count the amount of cups consumed and store the total water amount consumed in an array?
我收到一個字符串,我將其轉換為包含以mL為單位的水位的整數。 我在文本框中顯示水位,並在進度欄中顯示其進度。 我需要計算以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());
}
}
問題是,當水位變化至少400mL時,cup_count不會更新。 我怎樣才能解決這個問題?
編輯:對不起,我的困惑是我的期望輸入是整數'number',該整數隨着水位的增加而更新,它將介於0mL至500mL之間。
我正在嘗試記錄消耗的杯水量,如果用戶喝400mL或更多,則會發生這種情況,並將其存儲在變量“ cup_count”中,以便可以將其顯示在XML的EditText框“ cupcount”中。 我還想記錄以mL為單位的水消耗總量,因此我可以通知用戶在一定時間內他們沒有足夠的水,這將存儲在變量“ cup_amount”中。 我什至不確定使用數組是否是存儲這些值的最佳方法。
編輯:
我不確定這是否是您嘗試實現的目標,但是您可以執行以下操作:
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.