JAVA - 字符的逻辑运算

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5479499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:18:49  来源:igfitidea点击:

JAVA - logical operations on chars

javacharoperators

提问by S_Wheelan

I am creating a file reading program. I need to filter out any char that is not '0-9' or '.'.

我正在创建一个文件读取程序。我需要过滤掉任何不是“0-9”或“.”的字符。

Any char other then these needs to trigger an IF statement.

除了这些之外的任何字符都需要触发 IF 语句。

Here is what I tried -

这是我尝试过的-

if  ( ( ((char)c < '0') || ((char)c > '9') ) || ((char)c != '.') )

or-

或者-

( ( ((char)c != '0' ) || ((char)c != '.' ) || ((char)c != '1' ) || ((char)c != '2' ) || ((char)c != '3' ) || ((char)c != '4' ) || ((char)c != '5' ) || ((char)c != '6' ) || ((char)c != '7' ) || ((char)c != '8' ) || ((char)c != '9' ) ))

neither of which worked.

两者都没有奏效。

回答by Speck

if(Character.isDigit(c) || c == '.')
{

}

回答by MByD

Any char that is not '.' will cause this if statement to be true, to fix it (and I take the first as an example, but it applies also to the second):

任何不是 '.' 的字符 将导致此 if 语句为真,以修复它(我以第一个为例,但它也适用于第二个):

 if ( ( ((char)c < '0') || ((char)c > '9') ) && ((char)c != '.') )

alternatively, you can write

或者,你可以写

 if (!( ((char)c >='0' && (char) c <='9') || (char) c == '.') )