Java中的模运算,Android问题

发布于 2024-10-10 05:54:15 字数 721 浏览 10 评论 0原文

我根本无法弄清楚这一点。我这样做了:

int num = ((month-1)*30+day)%134;
              //a,b,c are just for the Log
        String a = String.valueOf(num);
        String b = String.valueOf(month);
        String c = String.valueOf(day);

        Log.v("variables",a+","+b+","+c);
        num ++;// don't want zero
        String stringnum = String.valueOf(num);
        Log.v("Index",stringnum);

月份是今天的月份,日期是今天(即 12 月 31 日 = 12,31)。在 Android 中,这取自 CALENDAR。 现在这个程序直到今天都运行良好。 1 月 1 日。发生的情况是数据库搜索从日期中获取的索引 num 的字符串。问题不在于数据库,而在于这段代码。 1 月 1 日自然是月=0,日=1,但是在 mod 134 之后我得到了,由 LogCat 提供:

01-01 12:07:38.554: VERBOSE/variables(6917): -29,0,1

但是 1mod134 != -29。我不明白-29从何而来。

I can't figure this out at all. I did:

int num = ((month-1)*30+day)%134;
              //a,b,c are just for the Log
        String a = String.valueOf(num);
        String b = String.valueOf(month);
        String c = String.valueOf(day);

        Log.v("variables",a+","+b+","+c);
        num ++;// don't want zero
        String stringnum = String.valueOf(num);
        Log.v("Index",stringnum);

month is todays month, day is today (i.e. december 31st = 12,31). In ANdroid this is taken from CALENDAR.
Now this program was working fine until today. Jan 1st. What happens is the DB searches for a string of index num, taken from the date. The problem isnt in the DB, its in this code.
Jan 1st is month=0, day = 1, naturally, but after mod 134 I get, courtesy of LogCat:

01-01 12:07:38.554: VERBOSE/variables(6917): -29,0,1

But 1mod134 != -29. I can't understand where -29 comes from.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

菊凝晚露 2024-10-17 05:54:15

0时,(month-1)*30-30

When month is 0, (month-1)*30 is -30.

探春 2024-10-17 05:54:15

对不起。发帖后我几乎立刻就明白了。月 = 0,因此月 -1 = -1。我假设月份会从 1 开始到 12,而不是从 0 到 11。抱歉浪费了大家的时间!

Sorry. I figured it out almost immediately after posting. month = 0, so month -1 = -1. I assumed month would start from 1 and go to 12, not zero to 11. Sorry for wasting anybody's time!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文