Java区分大小写字母数字和符号

java如何区分如题的四种东西呢?首先我想到的是利用ASCII码,因为不同的符号的ASCII码是不一样的。而要利用ASCII码区分的话,至少要知道A,Z,a,z,0,9的ASCII码,这样就可以利用ASCII码来区分了。 这是第一种方法。

package test;
import java.util.Scanner;
public class Differentiate{
    public static void main(String[] args){
        // A-65,Z-90,a-97,z-122,0-48,9-57
        Scanner sc = new Scanner(System.in);
        String nextLine = sc.nextLine();
        char[] charArray = nextLine.toCharArray();
        for(int i = 0;i<charArray.length;i++){
            int charAscii = (int)charArray[i];
            if(charAscii >=65 && charAscii <=90){
                System.out.print("大写字母:");
                System.out.println(charArray[i]);
            }else if(charAscii >=97 && charAscii <=122){
                System.out.print("小写字母:");
                System.out.println(charArray[i]);
            }else if(charAscii >= 48 && charAscii <= 57){
                System.out.print("数字:");
                System.out.println(charArray[i]);
            }else{
                System.out.print("符号:");
                System.out.println(charArray[i]);
            }
        }
    }
}        
复制代码

第二种方法,在网上查资料查到的,貌似在java里面对A-Z,a-z,0-9的char字符都有排序的,所以可以直接比较。

package test;
import java.util.Scanner;
public class Differentiate{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String nextLine = sc.nextLine();
        for (int i = 0; i < nextLine.length(); i++) {
            char c = nextLine.charAt(i);
            if (c >= 'A' && c <= 'Z') {
                System.out.print("max ");
                System.out.println(c);
            } else if (c >= 'a' && c <= 'z') {
                System.out.print("min ");
                System.out.println(c);
            } else if (c >= '0' && c <= '9') {
                System.out.print("数字 ");
                System.out.println(c);
            } else {
                System.out.print("符号");
                System.out.println(c);
            }
        }
    }
}
复制代码

还有一种方法,利用indexOf()函数,如果某个字符在字符串里面不存在,就会返回-1,所以只需要先做出A-Z,a-z,0-9三个字符串,然后每次用这三个字符串判断就行了

package test;
import java.util.Scanner;
public class Differentiate{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String nextLine = sc.nextLine();
        String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String lower = "abcdefghijklmnopqrstuvwxyz";
        String num = "0123456789";
        char[] charArray = nextLine.toCharArray();
        for(int i = 0;i<charArray.length;i++){
            if(upper.indexOf(charArray[i]) != -1){
                System.out.print("大写字母:");
                System.out.println(charArray[i]);
            }else if(lower.indexOf(charArray[i]) != -1){
                System.out.print("小写字母:");
                System.out.println(charArray[i]);
            }else if(num.indexOf(charArray[i]) != -1){
                System.out.print("数字:");
                System.out.println(charArray[i]);
            }else{
                System.out.print("符号:");
                System.out.println(charArray[i]);
            }
        }
    }
}
复制代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值