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

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

这是第一种方法。

 1 package test;
 2 import java.util.Scanner;
 3 public class Differentiate{
 4     public static void main(String[] args){
 5         // A-65,Z-90,a-97,z-122,0-48,9-57
 6         Scanner sc = new Scanner(System.in);
 7         String nextLine = sc.nextLine();
 8         char[] charArray = nextLine.toCharArray();
 9         for(int i = 0;i<charArray.length;i++){
10             int charAscii = (int)charArray[i];
11             if(charAscii >=65 && charAscii <=90){
12                 System.out.print("大写字母:");
13                 System.out.println(charArray[i]);
14             }else if(charAscii >=97 && charAscii <=122){
15                 System.out.print("小写字母:");
16                 System.out.println(charArray[i]);
17             }else if(charAscii >= 48 && charAscii <= 57){
18                 System.out.print("数字:");
19                 System.out.println(charArray[i]);
20             }else{
21                 System.out.print("符号:");
22                 System.out.println(charArray[i]);
23             }
24         }
25     }
26 }

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

 1 package test;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Differentiate{
 6     public static void main(String[] args) {
 7         Scanner sc = new Scanner(System.in);
 8         String nextLine = sc.nextLine();
 9         for (int i = 0; i < nextLine.length(); i++) {
10             char c = nextLine.charAt(i);
11             if (c >= 'A' && c <= 'Z') {
12                 System.out.print("max ");
13                 System.out.println(c);
14             } else if (c >= 'a' && c <= 'z') {
15                 System.out.print("min ");
16                 System.out.println(c);
17             } else if (c >= '0' && c <= '9') {
18                 System.out.print("数字 ");
19                 System.out.println(c);
20             } else {
21                 System.out.print("符号");
22                 System.out.println(c);
23             }
24         }
25     }
26 }

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

 1 package test;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Differentiate{
6 public static void main(String[] args) { 7 Scanner sc = new Scanner(System.in); 8 String nextLine = sc.nextLine(); 9 String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 10 String lower = "abcdefghijklmnopqrstuvwxyz"; 11 String num = "0123456789"; 12 char[] charArray = nextLine.toCharArray(); 13 for(int i = 0;i<charArray.length;i++){ 14 if(upper.indexOf(charArray[i]) != -1){ 15 System.out.print("大写字母:"); 16 System.out.println(charArray[i]); 17 }else if(lower.indexOf(charArray[i]) != -1){ 18 System.out.print("小写字母:"); 19 System.out.println(charArray[i]); 20 }else if(num.indexOf(charArray[i]) != -1){ 21 System.out.print("数字:"); 22 System.out.println(charArray[i]); 23 }else{ 24 System.out.print("符号:"); 25 System.out.println(charArray[i]); 26 } 27 } 28 } 29 }

 

posted @ 2016-02-28 00:15  L_Sivan  阅读(3878)  评论(0编辑  收藏  举报