简体   繁体   English

Java-charAt得到错误的字符

[英]Java - charAt getting wrong character

I am trying to make an algorithm that will do the following: 我正在尝试一种算法,可以执行以下操作:

64 
797
  7
===
 79

that will take 7, multiply it by 7, and then write the last digit of the answer down and then multiply it by seven and add the first digit of 7 times 7, and so on - so if you do it three times (write it down), you get what I wrote up there as a multiplication. 这将需要7,再乘以7,然后写下答案的最后一位数字,然后再乘以7,再将答案的第一位数字与7乘以7,依此类推-如果您这样做了三遍(写出来) (向下),您得到的是我在那儿写的乘法。

I got a not good code, instead of showing (for example) whats above in this form: 我得到的代码不好,没有以这种形式显示(例如)上述内容:

7,9,4
9,7,6

and so on I get something like this: 等等,我得到这样的东西:

7, 9, 52
9, 55, 54

My code: 我的代码:

    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;
    }

As far as I can see the problem is in the charAt, because obviously the first character of 7*7 is not 52. 据我所知,问题出在charAt,因为显然7 * 7的第一个字符不是52。

EDIT Now that the code is working fine I have another problem. 编辑现在代码可以正常工作了,我还有另一个问题。 I tried my new working code (put tens as the int value of the string value of the char instead of just the char), I have another problem. 我尝试了新的工作代码(将tens用作char的字符串值的int值,而不只是char),我还有另一个问题。 Hooray! 万岁!

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

My code now is as the same as the old code just with the tens fixed. 现在,我的代码与旧代码相同,只是固定了数十个。 But now, I am getting 15 for a number. 但是现在,我的数字是15。 That is supposed to be a digit. 那应该是个数字。 Whats wrong? 怎么了? I honestly don't know if the code I wrote will fullfil my purpose. 老实说,我不知道我写的代码是否能满足我的目的。 What code will? 用什么代码?

I strongly suspect that the problem is the type of tens . 我强烈怀疑问题出在tens种类型上。 You haven't shown this in your code, but I suspect it's int . 您尚未在代码中显示此内容,但我怀疑它是int

So this line: 所以这行:

tens = (upper*7+"").charAt(0);

takes the first character from a string, and then stores it in an int . 取的第一个字符从字符串,然后将其存储在一个int So for example, the character '4' is Unicode 52 ('0') is 48. The conversion to int just converts the UTF-16 code unit from an unsigned 16-bit value to a signed 32-bit value. 因此,例如,字符“ 4”为Unicode 52(“ 0”)为48。转换为int只是将UTF-16代码单元从无符号的16位值转换为有符号的32位值。

You're then displaying the value of tens - but if tens is indeed an int , it's going to display that as a number . 然后,您将显示tens的值-但如果tens确实是一个int ,它将显示为数字

As far as I can see the problem is in the charAt, because obviously the first character of 7*7 is not 52. 据我所知,问题出在charAt,因为显然7 * 7的第一个字符不是52。

Well, the first character of the string representation of 7*7 will be '4'. 好吧,字符串表示形式7 * 7的第一个字符将是'4'。 When that's been converted to an int , you'll see that as 52. 将其转换为int ,您将看到52。

If you just want tens as a char , you should declare it as type char . 如果只想将tens用作char ,则应将其声明为char类型。 You shouldn't do arithmetic with that value of course - but then when you display it, you'll see 4 displayed, because the string conversion will still treat it just as a character instead of as a number. 当然,您不应该对该值进行算术运算-但是,当您显示该值时,会看到显示4,因为字符串转换仍会将其视为字符而不是数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM