用java中的char变量计算

Calculating with the char variable in java(用java中的char变量计算)
本文介绍了用java中的char变量计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个学校作业,其中一项任务是解释许多微小的计算,并解释为什么 java 给你它给你的输出..

I have an assignment for school, and one of the tasks is to explain a lot of tiny calculations and explaining why java gives you the output it gives you..

其中一项计算是:

1 + '2' + 3

1 + '2' + 3

这对我来说是一个词汇错误,因为老师为我的系统使用了错误的撇号",但我已经和其他同学交谈过,他们告诉我他们得到了一个实际的输出,所以我开始阅读它,并发现它应该表示一个 char 变量,并且我还发现了系统特定类型,所以我更改了符号以适用于我的系统,现在我得到了答案 54..

which for me gives a lexical error, as the teacher used the wrong "apostrophes" for my system, but I've talked to other fellow students and they told me they got an actual output, so I started reading about it, and found out that it is supposed to signify a char variable, and I also found out about the system specific types, so I changed the signs to work for my system, and now I get the answer 54..

我看不到其中的逻辑,我尝试用 char 变量在 Google 上添加/计算/数学,但没有找到任何可以很好地解释它的东西..

and I cannot see the logic in it, and I've tried to google adding/calculating/math with char variables, and have found nothing that explains it well..

所以我求助于编码人员,有一天我可能会成为其中的一员,以帮助我理解其中的逻辑..

So I turn to you, the people of coding, that I one day might be a part of to help me understand the logic of this..

这开始是一个家庭作业,我可能只是回答它给出了一个词法错误,因为我的编译器不理解这个符号,但现在它已经达到了我的好奇心,我真的很想知道如何java设法得到这个答案..

this started out as a homework assignment that I probably could have gotten through by just answering that it gives a lexical error because my compiler doesn't understand the symbol, but now it's peaked my curiosity, and I really want to know how java manages to get this answer..

感谢您对此事的任何帮助!:)

thank you for any help on the matter! :)

我可以看到我无法制作作业"标签,所以我希望我可以把它放在这里:)

I can see that I couldn't make a 'homework' tag, so I hope it's okay that I put it here :)

推荐答案

在 Java 中,chars 可以通过 UTF-16 直接映射到 ints.但是,对于大多数常见字符,将 char 值转换为 int 会在 ascii 表中生成它的索引.+ 不是对字符的操作,而是对整数的操作.因此,java 采用 2 并在思考我无法添加这个"之后,意识到如果将其强制转换为 int,它可以添加它.

In Java, chars have a direct mapping to ints by UTF-16. For most common characters, though, casting a char value to an int yields its index on the ascii table. + isn't an operation on chars, but it is an operation for ints. Therefore, java is taking the 2 and after thinking "I can't add this", realizes it can add it if it casts it to an int.

正如您在表中看到的,2"的索引为 50,因此 1 + 50 + 3 = 54.

As you can see in the table, '2' has a index of 50, thus 1 + 50 + 3 = 54.

这篇关于用java中的char变量计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)