简体   繁体   English

Java - 两个字符,如何识别哪个是“?”

[英]Java - two chars, how recognize which one is '?'

I have two char ' ' and '?'.我有两个字符 ' ' 和​​ '?'。 How to recognize them from java?如何从java中识别它们? How to check which one is natural '?'如何检查哪个是天然的“?” and which one is not?哪个不是?


Character Encoding:字符编码:

Short: In computers, each character is represented by a set of binary digits.简短:在计算机中,每个字符都由一组二进制数字表示。 The digits for representing a character are determined by the character encoding that you have chosen to use.代表字符的数字由您选择使用的字符编码决定。 For enrichment:为了丰富:

https://en.wikipedia.org/wiki/Character_encoding https://en.wikipedia.org/wiki/Character_encoding


Solution:解决方案:

To the two characters you've introduced here, there are completely different numeric values.对于您在这里介绍的两个字符,有完全不同的数值。 I don't know what character encoding you are using (I don't need to know), so try to print their numeric value by converting them to an integer and look at the result.我不知道您使用的是什么字符编码(我不需要知道),因此尝试通过将它们转换为整数来打印它们的数值并查看结果。

As you can see, their values are different, and incidentally, also their appearance.如您所见,它们的值不同,顺便说一句,它们的外观也不同。 Therefore, it follows that all that needs to be done is to compare these two characters (Which is actually to compare both numeric values).因此,接下来需要做的就是比较这两个字符(实际上是比较两个数值)。

if ('�' == '?')
    System.out.println("Always False!");

Can you see the problem here?你能看出这里的问题吗? Yes, this condition will be always FALSE .是的,此条件将始终为FALSE As a result, the instruction inside the if statement scope is considered dead code .因此, if 语句范围内的指令被视为死代码

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

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