【发布时间】:2014-05-21 15:03:31
【问题描述】:
我正在尝试制作一个算法来执行以下操作:
64
797
7
===
79
这需要 7,乘以 7,然后写下答案的最后一位数字,然后乘以 7,然后加上 7 的第一个数字乘以 7,依此类推 - 所以如果你做 3 次(把它写下来),你会得到我在那里写的乘法。
我得到了一个不好的代码,而不是以这种形式显示(例如)上面的内容:
7,9,4
9,7,6
等等我得到这样的东西:
7, 9, 52
9, 55, 54
我的代码:
for(int i = 0; i<3; i++){//Run the code three times
temp=upper*7%10+tens;//get the temp but keeping the upper because I am going to need it for the tens part
tens=(upper*7+"").charAt(0);//get the first character of upper*7
System.out.println(upper+", "+temp+", "+tens);
upper=temp;
}
据我所知,问题出在 charAt 中,因为显然 7*7 的第一个字符不是 52。
编辑 现在代码工作正常,我还有另一个问题。我尝试了我的新工作代码(将 tens 作为 char 的字符串值的 int 值,而不仅仅是 char),我还有另一个问题。万岁!
Last tens: 0 Now's plain number:7, New:9, Tens:4
Last tens: 4 Now's plain number:9, New:7, Tens:6
Last tens: 6 Now's plain number:7, New:15, Tens:4
Last tens: 4 Now's plain number:15, New:9, Tens:1
Last tens: 1 Now's plain number:9, New:4, Tens:6
我的代码现在与旧代码相同,只是固定了十位。但现在,我得到了 15 个数字。那应该是一个数字。怎么了?老实说,我不知道我写的代码是否能实现我的目的。什么代码?
【问题讨论】:
-
如果您展示一个简短但完整的程序来演示问题,那将非常有帮助。
charAt确实有效... -
并尝试更好地解释您的问题。当我读到你的问题时,我的大脑爆炸了。
-
你认为第一个字符应该是什么?你查阅过 ASCII 图表吗?