【问题标题】:While loop to reduce int in androidWhile循环减少android中的int
【发布时间】:2015-06-22 17:52:57
【问题描述】:

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

这是我用来减少 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 toasts 为“29”,所以它似乎没有减少。

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

【问题讨论】:

  • 你能修复你的缩进吗?
  • 也许你应该了解模运算符 (%) 是什么以及做什么...
  • while (day &lt; 7) 确定吗?
  • to show what day of that program week the user is in. 你真的只需要所有这些机械 to show the current weekday吗? stackoverflow.com/a/23266940/2649012

标签: java android


【解决方案1】:

while 语句是错误的,你应该检查 while(day &gt; 7) -&gt; do this 而不是 while(day &lt; 7) 。代码 -

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

【讨论】:

  • 哇。哇。我是个假人。感谢您指出这一点,而不是因为我是个笨蛋而炸毁我的位置。干杯!
  • 建议您接受答案(通过单击左侧的勾号),这样其他人就不必回答这个问题了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-15
  • 1970-01-01
相关资源
最近更新 更多