isDigit() 对于字母返回 true
当运行以下程序并输入一个字母时,其中一个输出窗口显示该字母是数字,而实际上它显然不是。为什么?
import javax.swing.JOptionPane;
/**
* This program demonstrates some of the Character
* class's character testing methods
*
*
*/
public class CharacterTest {
public static void main(String[] args){
String input; //To hold the user's input
char ch; //To hold a single character
//Get a character from the user and store
//it in the ch variable
input=JOptionPane.showInputDialog("Enter "+
"any single character.");
ch= input.charAt(0);
//Test the character
if(Character.isLetter(ch)){
JOptionPane.showMessageDialog(null, "This is a letter.");
}
if(Character.isDigit(ch));{
JOptionPane.showMessageDialog(null, "Thit is a digit.");
}
if(Character.isLowerCase(ch)){
JOptionPane.showMessageDialog(null, "That is a lowercase"+
" letter");
}
if(Character.isUpperCase(ch)){
JOptionPane.showMessageDialog(null, "That is an uppercase"+
" letter");
}
if(Character.isSpaceChar(ch)){
JOptionPane.showMessageDialog(null, "That is an uppercase"+
" letter");
}
if(Character.isWhitespace(ch)){
JOptionPane.showMessageDialog(null, "That is an uppercase"+
" letter");
}
System.exit(0);
}
}
When running the following program and inputing a letter, one of the output windows says that the letter is a digit when it is clearly not. Why?
import javax.swing.JOptionPane;
/**
* This program demonstrates some of the Character
* class's character testing methods
*
*
*/
public class CharacterTest {
public static void main(String[] args){
String input; //To hold the user's input
char ch; //To hold a single character
//Get a character from the user and store
//it in the ch variable
input=JOptionPane.showInputDialog("Enter "+
"any single character.");
ch= input.charAt(0);
//Test the character
if(Character.isLetter(ch)){
JOptionPane.showMessageDialog(null, "This is a letter.");
}
if(Character.isDigit(ch));{
JOptionPane.showMessageDialog(null, "Thit is a digit.");
}
if(Character.isLowerCase(ch)){
JOptionPane.showMessageDialog(null, "That is a lowercase"+
" letter");
}
if(Character.isUpperCase(ch)){
JOptionPane.showMessageDialog(null, "That is an uppercase"+
" letter");
}
if(Character.isSpaceChar(ch)){
JOptionPane.showMessageDialog(null, "That is an uppercase"+
" letter");
}
if(Character.isWhitespace(ch)){
JOptionPane.showMessageDialog(null, "That is an uppercase"+
" letter");
}
System.exit(0);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这意味着:
没有任何条件,所以它总是打印它是一个数字。
顺便说一句,空格和空白(有趣的是 Java 如何区分两者)都不是“大写字母”。
That means:
without any condition, so it will always print that it's a digit.
By the way, neither spaces nor white spaces (funny how Java distinguishes between the two) are "uppercase letters".