我已经完成了该程序,但是如果是负数,它会显示特殊字符,但是我不希望它显示数字。
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值或其他建议?
最佳答案
根据注释,如果输入-9
,则代码将仅包含-
。您只需检查负号即可
public static void main(String args[])
{
Scanner scanner=new Scanner(System.in);
char char1 =scanner.next().charAt(0);
if((char1>=48 && char1<=57) || char1 == 45)
{
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");
}
}