我正在尝试制作一种算法来执行以下操作:
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 个数字。那应该是一个数字。怎么了?老实说,我不知道我写的代码是否能实现我的目的。什么代码会?