題目:輸入一行字符,分別統計出其中英文字母、空格、數字和其它字符的個數。import java.util.*;public class lianxi07 {public static void main(String[] args){int digital = 0;int character = 0;int other = 0;int blank = 0;char[] ch = null;Scanner sc = new Scanner(System.in);String s = sc.nextLine();--------------------------------------------下面的邏輯不太理解-----------------------------------------------ch = s.toCharArray();for(int i=0; i<ch.length; i++) {if(ch >= '0' && ch <= '9') {digital ++;} else if((ch >= 'a' && ch <= 'z') || ch > 'A' && ch <= 'Z') {character ++;} else if(ch == ' ') {blank ++;} else {other ++;}}---------------------------------------------------------------------------------------------------------------------System.out.println("數字個數: " + digital);System.out.println("英文字母個數: " + character);System.out.println("空格個數: " + blank);System.out.println("其他字符個數:" + other );}}
添加回答
舉報
0/150
提交
取消