两个 ASCII 值之间的字符 Java
A char between two ASCII values Java
我是 Java 的初学者,我目前正在编写一个代码,其中所有字符符号的打印方式相同,但字母的打印方式不同。如何排除符号的 ASCII 值?是否可以这样做(其中32和64等值表示与字符对应的ASCII值):
char notLetter = (originalMessage.charAt(i));
if ((32 <= notLetter <= 64) || (91 <= notLetter <= 96) || (123 <= notLetter <= 126)){
codedMessage += notLetter;
}
或者有更简单的方法吗?谢谢
编辑:当我尝试这段代码时,我收到以下错误:“<= cannot be applied to boolean, int”
char notLetter = (originalMessage.charAt(i));
if ((32 <= notLetter && notLetter <= 64) || (91 <= notLetter && notLetter <= 96) || (123 <= notLetter && notLetter<= 126)){
codedMessage += notLetter;
}
试试这个。
32 <= notLetter >= 64
这行不通有两个原因:
- 不支持该格式。
- 大于 32 且大于 64 的数字就是任何大于 64 的数字
我会使用带有 isAlphabetic() 函数的字符对象 Class:
http://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#isAlphabetic(int)
ASCII 和 Unicode 应该匹配。
32 <= notLetter >= 64
在 java 中是不合法的,但是 32 <= notLetter && notLetter >= 64
是允许的。然而,这也永远不会是真的——你是说 32 <= notLetter && notLetter <= 64
吗?
需要注意的另一件事可能对您有所帮助:您实际上可以在两边使用 <=
和一个字符:
(' ' <= notLetter && <= '@')
如果我明白你想做什么,这会做你想做的事:
char notLetter = (originalMessage.charAt(i));
if ((' ' <= notLetter && notLetter <= '@') || ('[' <= notLetter && notLetter <= '`') || ('{' <= notLetter && notLetter <= '~')){
codedMessage += notLetter;
}
我不确定你到底想做什么,但这里有一些一般信息。
- 使用像
'a'
这样的 char
文字,而不是它们的 int
值。这使得程序更容易理解。
- 在大多数情况下,您应该使用
StringBuilder
而不是字符串连接。
- Java 不支持像
2 <= a <= 5
这样的表达式,所以你必须用 2 <= a && a <= 5
代替。
以下代码打印 , !
String x = "Hello, World!";
StringBuilder sb = new StringBuilder();
for (char c : x.toCharArray()) {
if (!(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')) {
sb.append(c);
}
}
System.out.println(sb.toString());
我是 Java 的初学者,我目前正在编写一个代码,其中所有字符符号的打印方式相同,但字母的打印方式不同。如何排除符号的 ASCII 值?是否可以这样做(其中32和64等值表示与字符对应的ASCII值):
char notLetter = (originalMessage.charAt(i));
if ((32 <= notLetter <= 64) || (91 <= notLetter <= 96) || (123 <= notLetter <= 126)){
codedMessage += notLetter;
}
或者有更简单的方法吗?谢谢
编辑:当我尝试这段代码时,我收到以下错误:“<= cannot be applied to boolean, int”
char notLetter = (originalMessage.charAt(i));
if ((32 <= notLetter && notLetter <= 64) || (91 <= notLetter && notLetter <= 96) || (123 <= notLetter && notLetter<= 126)){
codedMessage += notLetter;
}
试试这个。
32 <= notLetter >= 64
这行不通有两个原因:
- 不支持该格式。
- 大于 32 且大于 64 的数字就是任何大于 64 的数字
我会使用带有 isAlphabetic() 函数的字符对象 Class: http://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#isAlphabetic(int)
ASCII 和 Unicode 应该匹配。
32 <= notLetter >= 64
在 java 中是不合法的,但是 32 <= notLetter && notLetter >= 64
是允许的。然而,这也永远不会是真的——你是说 32 <= notLetter && notLetter <= 64
吗?
需要注意的另一件事可能对您有所帮助:您实际上可以在两边使用 <=
和一个字符:
(' ' <= notLetter && <= '@')
如果我明白你想做什么,这会做你想做的事:
char notLetter = (originalMessage.charAt(i));
if ((' ' <= notLetter && notLetter <= '@') || ('[' <= notLetter && notLetter <= '`') || ('{' <= notLetter && notLetter <= '~')){
codedMessage += notLetter;
}
我不确定你到底想做什么,但这里有一些一般信息。
- 使用像
'a'
这样的char
文字,而不是它们的int
值。这使得程序更容易理解。 - 在大多数情况下,您应该使用
StringBuilder
而不是字符串连接。 - Java 不支持像
2 <= a <= 5
这样的表达式,所以你必须用2 <= a && a <= 5
代替。
以下代码打印 , !
String x = "Hello, World!";
StringBuilder sb = new StringBuilder();
for (char c : x.toCharArray()) {
if (!(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')) {
sb.append(c);
}
}
System.out.println(sb.toString());