while循环减少android中的int

时间:2015-06-22 17:52:57

标签: java android

我正在尝试从SharedPreferences获取一个跟踪应用程序活动时间的int,并将其减少到小于7的int,以显示用户所在的该程序周的哪一天。< / p>

这是我用来减少int的内容,(我正在尝试使用while循环进入并减去7直到int小于7. IF语句被添加以查看是否int减少了,它显示错误的字符串)

public void methodNameHere() {
        SharedPreferences onvalue = getApplicationContext().getSharedPreferences("TRAININGDAY", MODE_APPEND);
        int day = (int) onvalue.getLong("DAY", 0);
        SharedPreferences.Editor edit = onvalue.edit();

        while (day < 7) {
            day = day - 7;
        }

        if (day < 7) {
            String da = Integer.toString(day);
            Toast.makeText(CardioStart.this, da, Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(CardioStart.this, "f?*k", Toast.LENGTH_LONG).show();
        }
    }

这就是int的来源:

private void dayOfTraining() {

        TextView day = (TextView) findViewById(R.id.dayoftraining);
        date.setTimeInMillis(millidate);

        Calendar someday = Calendar.getInstance();
        someday.set(2015, 4, 25);

        Calendar today = Calendar.getInstance();
        Long millidate = someday.getTimeInMillis();
        long timeTwo = today.getTimeInMillis();
        long oneDay = 1000 * 60 * 60 * 24;
        long delta = (timeTwo - millidate) / oneDay;
        long actualday = delta + 1;
        day.setText(Long.toString(actualday));

        SharedPreferences onvalue = getApplicationContext().getSharedPreferences("TRAININGDAY", MODE_APPEND);
        SharedPreferences.Editor edit = onvalue.edit();
        edit.putLong("DAY", actualday);
        edit.commit();

    }

String da toast as“29”所以它似乎没有减少。

我觉得我错过了一些简单的东西,或者我没有真正掌握while循环的使用。任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:1)

while语句错误,您应该检查while(day > 7) -> do this而不是while(day < 7)。代码 -

while (day > 7)
{
day = day - 7;
}