isDigit() 对于字母返回 true

发布于 2024-11-13 12:00:35 字数 1377 浏览 4 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枫林﹌晚霞¤ 2024-11-20 12:00:35
if(Character.isDigit(ch));{
        JOptionPane.showMessageDialog(null, "Thit is a digit.");
    }

这意味着:

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

没有任何条件,所以它总是打印它是一个数字。

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

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

That means:

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

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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文