gpt4 book ai didi

java - 程序显示该值是否为字母,然后打印 "Alphabet"如果它是数字,则打印 "Digit",对于其他字符打印 "Special Character"?

转载 作者:行者123 更新时间:2023-12-02 11:17:25 25 4
gpt4 key购买 nike

我已经完成了该程序,但如果是负数,它会显示特殊字符,但我不希望它显示数字。

public class DigitAlphabetSpecialCharacter {
public static void main(String args[])
{
Scanner scanner=new Scanner(System.in);
char char1 =scanner.next().charAt(0);
if(char1>=48 && char1<=57)
{
System.out.print("char is Digit");

}
else if((char1>='a' && char1<='z')||(char1>='A' && char1<='Z'))
{
System.out.print("char is Alphabet");
}
else
{
System.out.print("char is special character");

}
}

}谁能告诉我如何使用负数的 ASCII 值或替代建议吗?

最佳答案

字符不能包含负值,因为它需要两个字符。并且 char 变量只能存储单个字符。

您可以使用 Character 类中预定义的函数,而不是使用 ASCII 值。

关于java - 程序显示该值是否为字母,然后打印 "Alphabet"如果它是数字,则打印 "Digit",对于其他字符打印 "Special Character"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50171876/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com