添加 char 和 int [英] Adding char and int

查看:20
本文介绍了添加 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆