简体   繁体   English

如何使用Java Scanner读取命题逻辑符号作为输入?

[英]How to read propositional logic symbols as input using Java Scanner?

 Scanner in = new Scanner(System.in,"UTF-8");
 System.out.println(in.next());

If I paste , I receive ? 如果我粘贴 ,我会收到? as output to the console. 作为控制台的输出。 Can someone explain what I can do to properly read logic symbols like this? 有人可以解释我能做些什么来正确读取这样的逻辑符号? I'm using NetBeans 8.0.1. 我正在使用NetBeans 8.0.1。

Thanks. 谢谢。

The problem is not with entering the character, but rather with printing it to console. 问题不在于输入角色,而在于将其打印到控制台。 Your console does not appear to support the code point \∧ of , so it prints a question mark instead. 控制台似乎不支持的代码点\∧ ,所以它打印一个问号代替。

You should test if console lets you input correctly by printing the numeric representation of the character that you read, like this: 您应该测试控制台是否允许您通过打印您读取的字符的数字表示来正确输入 ,如下所示:

Scanner in = new Scanner(System.in,"UTF-8");
String s = in.next();
if (s.length() != 0) {
    System.out.println((int)s.charAt(0));
}

If 8743 gets printed, you could process the character internally: comparisons like this 如果打印8743 ,您可以在内部处理角色:比较如下

if (s.equals("∧")) {
    ...
}

will work correctly. 会正常工作。

Otherwise, you should switch to using characters from the first code page, ie ^ instead of 否则,您应该切换到使用第一个代码页中的字符,即^而不是

I think your terminal encoding ( IDE or command line ) might not be UTF-8 so when it converts you see the difference. 我认为您的终端编码(IDE或命令行)可能不是UTF-8,因此当它转换时,您会看到差异。

I just tried your code and got ∧ back. 我刚尝试了你的代码并得到了∧回来。

Do an echo $LC_CTYPE or echo $LANG to check it. 使用echo $ LC_CTYPE或echo $ LANG进行检查。

Read this How to read a text file with mixed encodings in Scala or Java? 阅读本文如何在Scala或Java中读取带有混合编码的文本文件? and http://docs.oracle.com/javase/7/docs/api/java/nio/charset/CharsetDecoder.html http://docs.oracle.com/javase/7/docs/api/java/nio/charset/CharsetDecoder.html
for working with different encodings. 用于处理不同的编码。

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

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