添加char和int [英] Adding char and int
本文介绍了添加char和int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
根据我的理解,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.
这篇关于添加char和int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文