简体   繁体   English

乘法变量时的错误答案

[英]Wrong answer when multiplying variables

I'm trying to multiply two variables. 我试图将两个变量相乘。 One is an int and the other is inside a char array. 一个是int,另一个是char数组。

My code is this 我的代码是这样的

int biggestProduct = 1;
int currProduct = 1;
char[] array = num.toCharArray();

for (int x = 0; x < array.length; x++) {
    if (x < 5) {
        System.out.println(currProduct + " * " + array[x] + " = " + currProduct * array[x]);
        currProduct *= array[x];
    }
}

return biggestProduct;

The problem is with the currProduct*= array[x] 问题出在currProduct*= array[x]

This is my output when I print it out: 打印出来时这是我的输出:

1 * 7 = 55
55 * 3 = 2805
2805 * 1 = 137445
137445 * 6 = 7422030
7422030 * 7 = 408211650

Why isn't it multiplying correctly? 为什么它不能正确倍增?

The problem is that the numeric value of the char 7 is not 7 but 55 . 问题是char 7的数值不是7而是55

Because in Java, the char data type is a single 16-bit Unicode character (see encoding table below). 因为在Java中,char数据类型是单个16位Unicode字符(请参阅下面的编码表)。

在此输入图像描述

If you take a look at this table, you see that 7 is encoded as 0x0037 = 3*16^1 + 7 = 55 . 如果你看一下这个表,你会看到7被编码为0x0037 = 3*16^1 + 7 = 55

If you want to take the real numeric value of your character, you can use Character.getNumericValue(char ch) : 如果要获取角色的实际数值,可以使用Character.getNumericValue(char ch)

 char ch = '7';
 int number = Character.getNumericValue(ch);
 System.out.print(number); //print 7

So to edit your code, it will looks like : 因此,要编辑代码,它将如下所示:

        String num = "73167";
        int biggestProduct = 1;
        int currProduct = 1;
        char[] array = num.toCharArray();

        for (int x = 0; x < array.length; x++) {
            if (x < 5) {
                System.out.println(currProduct + " * " + array[x] + " = " + currProduct * Character.getNumericValue(array[x]));
                currProduct *= Character.getNumericValue(array[x]);
            }

        }

Output : 输出:

1 * 7 = 7
7 * 3 = 21
21 * 1 = 21
21 * 6 = 126
126 * 7 = 882

Verification : 7*3*1*6*7 = 882 验证: 7*3*1*6*7 = 882

Try like this: 试试这样:

currProduct *= (int) array[x];

Check here for what the char values usually represent. 在这里为了什么char值通常代表。 You will see that if you want your char to hold the numerical value 2, you have to actually assign 50: 你会看到如果你想让你的char保持数值2,你必须实际分配50:

char two = 50;
System.out.println(two); // prints out 2

The value of '7' is 55 because it is just another character like eg 'a', so its numeric value will be its ASCII code. '7'的值是55,因为它只是另一个字符,例如'a',因此它的数值将是它的ASCII码。 See here: http://www.asciitable.com/ 见这里: http//www.asciitable.com/
(Note that the used ASCII table can be also implementation dependant.) (请注意,使用的ASCII表也可以依赖于实现。)

The numeric value of '7' isn't 7, it's 55. This is like any character, for example the character 'A' is 65 '7'的数值不是7,它是55.这就像任何字符一样,例如字符'A'是65

For example 例如

public class Calendar
{
    public static void main(String[] args){
        char testChar='7';
        int testInt=testChar;

        System.out.println(testInt); //prints 55
    }
}

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

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