添加 char 和 int

2022-01-12 00:00:00 char int java

据我了解,char 是单个字符,即字母、数字、标点符号、制表符、空格或类似的东西.因此,当我这样做时:

To my understanding a char is a single character, that is a letter, a digit, a punctuation mark, a tab, a space or something similar. And therefore when I do:

char c = '1';
System.out.println(c);

输出 1 正是我所期望的.那么为什么当我这样做时:

The output 1 was exactly what I expected. So why is it that when I do this:

int a = 1;
char c = '1';
int ans = a + c;
System.out.println(ans);

我最终得到输出 50?

推荐答案

你得到它是因为它添加了 ASCII 值 字符.您必须先将其转换为 int.

You're getting that because it's adding the ASCII value of the char. You must convert it to an int first.

相关文章