isDigit()返回true为letter

时间:2011-06-01 19:19:46

标签: java wrapper character

当运行以下程序并输入一个字母时,其中一个输出窗口显示该字母显然不是数字。为什么呢?

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);

}
}

1 个答案:

答案 0 :(得分:8)

if(Character.isDigit(ch));{
        JOptionPane.showMessageDialog(null, "Thit is a digit.");
    }

这意味着:

JOptionPane.showMessageDialog(null, "Thit is a digit.");

没有任何条件,因此它将始终打印出它是一个数字。

顺便说一下,空格和空格都没有(有趣的是Java如何区分两者)是“大写字母”。