Java中的模运算,Android问题
我根本无法弄清楚这一点。我这样做了:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当
月
为0
时,(month-1)*30
为-30
。When
month
is0
,(month-1)*30
is-30
.对不起。发帖后我几乎立刻就明白了。月 = 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!