输入一行字符,分别统计出包含英文字母、空格、数字和其它字符的个数。
数据范围:输入的字符串长度满足
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String line = scanner.nextLine(); int letter = 0; int blank = 0; int digit = 0; int other = 0; char[] chars = line.toCharArray(); for (int i = 0; i < chars.length; i++) { if (Character.isLetter(chars[i])) { letter++; } else if (Character.isDigit(chars[i])) { digit++; } else if (chars[i] == ' ') { blank++; } else { other++; } } System.out.println(letter); System.out.println(blank); System.out.println(digit); System.out.println(other); }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 String val = in.nextLine(); char[] cs = val.toCharArray(); //字符 int a = 0; //空格 int b = 0; //数字 int c = 0; //其他 int d = 0; for (char v : cs) { if (v >= '0' && v <= '9') { c = c + 1; } else if (v == ' ') { b = b + 1; } else if ((v >= 'a' && v <= 'z') || (v >= 'A' && v <= 'Z') ) { a = a + 1; } else { d = d + 1; } } System.out.println(a); System.out.println(b); System.out.println(c); System.out.println(d); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextLine()) { // 注意 while 处理多个 case String s = in.nextLine(); int letter = 0; int blankSpace = 0; int num = 0; int other = 0; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (Character.isWhitespace(c)) { blankSpace++; } else if (Character.isLetter(c) ) { letter++; } else if (Character.isDigit(c)) { num++; } else { other++; } } System.out.println(letter); System.out.println(blankSpace); System.out.println(num); System.out.println(other); } } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); char [] arr = in.nextLine().toCharArray(); int [] a = new int[4]; for(int i = 0; i < arr.length; i++){ if(Character.isLetter(arr[i])){ a[0]++; }else if(arr[i] == ' '){ a[1]++; }else if(Character.isDigit(arr[i])){ a[2]++; }else{ a[3]++; } } System.out.println(a[0]+"\n"+a[1]+"\n"+a[2]+"\n"+a[3]); } }
public class HJ40 { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextLine()){ String str = in.nextLine(); count(str); // methodReplace(str); bytecodeCount(str); } } private static void methodReplace(String str) { int length = str.length(); String replaced = str.replaceAll("[\\u4E00-\\u9FA5A-Za-z]", ""); System.out.println(length - replaced.length()); length = replaced.length(); replaced = replaced.replaceAll(" ", ""); System.out.println(length - replaced.length()); length = replaced.length(); replaced = replaced.replaceAll("[0-9]", ""); System.out.println(length - replaced.length()); length = replaced.length(); System.out.println(length); } private static void count(String str) { int a = 0, e = 0, n = 0, o = 0; for (int i = 0; i < str.length(); i++) { char charred = str.charAt(i); String item = Character.toString(charred); if (item.matches("[\\u4E00-\\u9FA5A-Za-z]")) { a++; }else if (item.matches("[0-9]")){ n++; }else if (item.equals(" ")){ e ++; }else { o ++; } } System.out.println(a + "\n" + e + "\n" + n + "\n" + o); } private static void bytecodeCount(String str) { int a = 0, e = 0, n = 0, o = 0; for (int i = 0; i < str.length(); i++) { char item = str.charAt(i); //利用 字码表 if((item >= 0x4e00)&&(item <= 0x9fbb) || (item >= 65 && item <= 90) || (item >= 97 && item <= 122)) { a++; }else if (item >= 48 && item <= 57){ n++; }else if (Character.isSpaceChar(item)){ e++; }else { o ++; } } System.out.println(a + "\n" + e + "\n" + n + "\n" + o); } }
import java.util.Scanner; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line; while ((line = br.readLine()) != null) { // 存储不同字符的次数,可以用双值型数据类型hashMap HashMap<String, Integer> map = new HashMap<>(); for (int i = 0; i < line.length(); i++) { char c = line.charAt(i); if (Character.isLetter(c)) { String key = "character"; map.put(key, map.getOrDefault(key, 0) + 1); } else if (c == ' ') { String key = "space"; map.put(key, map.getOrDefault(key, 0) + 1); } else if (c >= '0' && c <= '9') { String key = "digital"; map.put(key, map.getOrDefault(key, 0) + 1); } else { String key = "other"; map.put(key, map.getOrDefault(key, 0) + 1); } } System.out.println(map.getOrDefault("character", 0)); System.out.println(map.getOrDefault("space", 0)); System.out.println(map.getOrDefault("digital", 0)); System.out.println(map.getOrDefault("other", 0)); } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.nextLine(); int eNum = 0; int sNum = 0; int mNum = 0; int oNum = 0; char[] chars = str.toCharArray(); for (char aChar : chars) { if((aChar >= 65 && aChar <= 90) || (aChar >= 97 && aChar <= 122)) { eNum++; }else if(aChar >= 48 && aChar <= 57) { mNum++; }else if(aChar == 32) { sNum++; }else oNum++; } System.out.println(eNum); System.out.println(sNum); System.out.println(mNum); System.out.println(oNum); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String a = in.nextLine(); int num = 0; int letter = 0; int kou = 0; int other = 0; for (int i = 0; i < a.length(); i++) { char cha = a.charAt(i); if (Character.isDigit(cha)) { num++; continue; } if (Character.isLetter(cha)) { letter++; continue; } if (Character.isSpaceChar(cha)) { kou++; continue; } other++; } System.out.println(letter); System.out.println(kou); System.out.println(num); System.out.println(other); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 String str=in.nextLine(); int c=0; int space=0; int math=0; int other=0; for(int i=0;i<str.length();i++){ if(Character.isLetter(str.charAt(i))){ c++; }else if(Character.isDigit(str.charAt(i))){ math++; }else if(Character.isSpace(str.charAt(i))){ space++; }else{ other++; } } System.out.println(c); System.out.println(space); System.out.println(math); System.out.println(other); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextLine()) { // 注意 while 处理多个 case String str=in.nextLine(); int a=str.length()-str.replaceAll("[a-zA-Z]","").length(); int b=str.length()-str.replaceAll(" ","").length(); int c=str.length()-str.replaceAll("[0-9]","").length(); int d=str.length()-a-b-c; System.out.println(a); System.out.println(b); System.out.println(c); System.out.println(d); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); int i = 0, j = 0, m = 0, n = 0; for (char c : str.toCharArray()) { if (Character.isLetter(c)) { i++; } else if (' ' == c) { j++; } else if (c >= '0' && c <= '9') { m++; } else { n++; } } System.out.println(i); System.out.println(j); System.out.println(m); System.out.println(n); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = br.readLine(); int[] count = new int[4]; for(int i = 0;i < line.length();i++){ char ch = line.charAt(i); if(Character.isLetter(ch)){ count[0]++; }else if(ch == ' '){ count[1]++; }else if(Character.isDigit(ch)){ count[2]++; }else{ count[3]++; } } for(int i = 0;i < 4;i++){ System.out.println(count[i]); } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { //获取字符串 String str = scanner.nextLine(); char[] chs=str.toCharArray(); int letterNum=0; int blankNum=0; int digitalNum=0; int othersNum=0; for (int i = 0; i < str.length(); i++) { if (Character.isLetter(chs[i])) { letterNum++; continue; } if (Character.isDigit(chs[i])) { digitalNum++; continue; } if (Character.isWhitespace(chs[i])) { blankNum++; continue; } othersNum++; } System.out.println(letterNum); System.out.println(blankNum); System.out.println(digitalNum); System.out.println(othersNum); } scanner.close(); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { public static void main(String[] args) throws Exception { /*BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = null;*/ Scanner scanner = new Scanner(System.in); while(scanner.hasNext()){ char[] toSum = scanner.nextLine().toCharArray(); int isEnglishChar = 0; int isSpaceChar = 0; int isDigitChar = 0; int isSpecialChar = 0; for(int i = 0;i < toSum.length;i++){ if(Character.isLetter(toSum[i])){ isEnglishChar++; }else if(toSum[i] == ' '){ isSpaceChar++; }else if(Character.isDigit(toSum[i])){ isDigitChar++; }else{ isSpecialChar++; } } System.out.println(isEnglishChar); System.out.println(isSpaceChar); System.out.println(isDigitChar); System.out.println(isSpecialChar); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); int a = 0, b = 0, c =0, d = 0; for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); if (Character.isLetter(ch)) { a++; } else if (ch == ' ') { b++; } else if (Character.isDigit(ch)) { c++; } else { d++; } } System.out.println(a); System.out.println(b); System.out.println(c); System.out.println(d); } }