用java编程统计用户从键盘输入的字符串中所包含的字母,数字和其他字符串的个数? 在JAVA中,编程统计用户从键盘输入的字符串中所包含的字母、...
Java\u7f16\u7a0b\u7edf\u8ba1\u7528\u6237\u4ece\u952e\u76d8\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u4e2d\u6240\u5305\u542b\u7684\u5b57\u6bcd,\u6570\u5b57\u548c\u5176\u4ed6\u5b57\u7b26\u7684\u4e2a\u6570\u3002 \u4e0d\u8981\u5199\u7684\u592a\u96be\uff0c\u521d\u5b66\u8005\u6c34\u5e73\uff01import java.util.Scanner;/** * \u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u6570\u5b57,\u5b57\u6bcd,\u7a7a\u683c,\u5176\u4ed6\u5b57\u7b26\u7684\u4e2a\u6570 * @author Administrator * */public class Data01 {public static void main(String[] args) {int englishCount = 0;// \u82f1\u6587\u5b57\u6bcd\u4e2a\u6570int spaceCount = 0;// \u7a7a\u683c\u4e2a\u6570int numCount = 0;// \u6570\u5b57\u4e2a\u6570int otherCount = 0;// \u5176\u4ed6\u5b57\u7b26\u4e2a\u6570Scanner sc = new Scanner(System.in);System.out.println("\u8bf7\u60a8\u8f93\u5165\u4e00\u884c\u5b57\u7b26\uff1a");String str = sc.nextLine();// \u53d6\u5f97\u63a7\u5236\u53f0\u8f93\u5165\u7684\u4e00\u884c\u5b57\u7b26char[] ch = str.toCharArray();// \u628a\u53d6\u9053\u7684\u5b57\u7b26\u4e32\u53d8\u6210\u4e00\u4e2achar\u6570\u7ec4for (int i = 0; i < ch.length; i++) {if (Character.isLetter(ch[i])) {// \u5224\u65ad\u662f\u5426\u4e3a\u5b57\u6bcdenglishCount++;} else if (Character.isSpaceChar(ch[i])) {// \u5224\u65ad\u662f\u5426\u4e3a\u7a7a\u683cspaceCount++;} else if (Character.isDigit(ch[i])) {// \u5224\u65ad\u662f\u5426\u4e3a\u6570\u5b57numCount++;} else {// \u4ee5\u4e0a\u90fd\u4e0d\u662f\u5219\u8ba4\u4e3a\u662f\u5176\u4ed6\u5b57\u7b26otherCount++;}}System.out.println("\u5b57\u6bcd\u7684\u4e2a\u6570\uff1a" + englishCount);System.out.println("\u6570\u5b57\u7684\u4e2a\u6570\uff1a" + numCount);System.out.println("\u7a7a\u683c\u7684\u4e2a\u6570\uff1a" + spaceCount);System.out.println("\u5176\u4ed6\u5b57\u7b26\u7684\u4e2a\u6570\uff1a" + otherCount);}}\u4ee3\u7801\u5982\u4e0a
\u544a\u8bc9\u4f60\u4e00\u4e2a\u601d\u8def\uff0c\u8bbe\u5b9a\u4e09\u4e2a\u53d8\u91cf\uff0c\u5206\u522b\u4ee3\u8868\u6570\u5b57\uff0c\u5b57\u6bcd\uff0c\u5176\u4ed6\u5b57\u7b26\u3002\u7528\u8f93\u5165\u6d41\u3002\u6bcf\u8f93\u5165\u4e00\u4e2a\u5b57\u7b26\uff0c\u5224\u65ad\u8fd9\u4e2a\u5b57\u7b26\u7684asc\u7801\uff0c\u5728\u591a\u5c11\u5230\u591a\u5c11\u4e4b\u95f4\u662f\u5b57\u6bcd\uff0c\u591a\u5c11\u5230\u591a\u5c11\u662f\u6570\u5b57\u3002\u6bcf\u6b21\u5224\u65ad\u4e00\u4e2a\uff0c\u5bf9\u5e94\u7684\u53d8\u91cf+1\uff0c\u6700\u540e\u5206\u522b\u8f93\u51fa\u53d8\u91cf
import java.util.Scanner;
public class Test {
public static void main(String [] args){
Scanner input = new Scanner(System.in);
System.out.print("输入字符串:");
String strs = input.next();
int number = 0;
int chara = 0;
int other = 0;
char [] chs = strs.toCharArray();
for(char c : chs){
if(c >= '0' && c <= '9'){
number++;
}else if(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' ){
chara++;
}else{
other++;
}
}
System.out.println("数字有:" + number + "个,字符有" + chara + "个,其他有:" + other + "个。" );
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class _1 {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String zifuchuan = new String("");
int hanzishu = 0;
int zimu = 0;
int kongge = 0;
int shuzi = 0;
int qita = 0;
System.out.print("请输入一行字符:");
BufferedReader stdin = new BufferedReader(new InputStreamReader(
System.in));
zifuchuan = stdin.readLine();
byte[] bytes = zifuchuan.getBytes();
for (int i = 0; i < bytes.length; i++) {
if ((bytes[i] >= 65 && bytes[i] <= 90)
|| (bytes[i] >= 97 && bytes[i] <= 122))
zimu++;
else if (bytes[i] == 32)
kongge++;
else if (bytes[i] >= 48 && bytes[i] <= 57)
shuzi++;
else if (bytes[i] < 0)
hanzishu++;
else
qita++;
}
System.out.println("字符串所占字节个数为:" + bytes.length);
System.out.println("汉字个数为:" + hanzishu / 2);
System.out.println("英文字母个数为:" + zimu);
System.out.println("空格个数为:" + kongge);
System.out.println("数字个数为:" + shuzi);
System.out.println("其他字符个数为:" + qita);
}
}
很简单的 用for循环遍历判断
绛旓細杩欎釜鏄鐞嗘枃浠剁殑绫籉ileUtil锛=== package org.xhome.leon.test;import java.io.*;public class FileUtil { FileReader fr;BufferedReader br;FileWriter fw;BufferedWriter bw;String source = "";public int upCaseNum = 0;public int lowerCaseNum = 0;public int numerNum = 0;public int oth...
绛旓細杩欓噷缁欐ゼ涓绘彁渚涗竴涓濊矾锛氬瓧绗︿釜鏁帮細杩欎釜涓嶇敤璇翠簡锛屾渶绠鍗 鍗曡瘝鏁帮細鍗曡瘝鏄敤锛 锛 锛 锛堢┖鏍硷級 . 杩欎簲涓鍙烽殧寮鐨勶紝浣犲彧瑕缁熻涓涓嬭繖鍑犱釜绗﹀彿鍑虹幇鐨勬鏁帮紝鐒跺悗鍔1灏辨槸鎬荤殑鍗曡瘝涓暟浜嗐傚綋鐒杈撳叆鐨勬枃瀛椾笉鑳芥湁杩炵画鐨勭鍙凤紝姣斿锛燂紵杩欐牱鐨勩傚彞瀛愭暟锛氭柟妗堝悓涓婄粺璁★紵 锛 锛 . 杩欏洓涓鍙凤紝鐒跺悗娆℃暟...
绛旓細public static void main(String[] args) { Scanner sc = new Scanner(System.in);int oddTotal = 0;int count = 0;while(true){ System.out.print("璇杈撳叆鏁板瓧:");int read = sc.nextInt();if(read == 10000)break;if((read & 1) == 1)oddTotal += read;count++;} System.out...
绛旓細瑕鐢↗ava浠庨敭鐩鑾峰彇10鍚嶅悓瀛︾殑鎴愮哗锛岃绠楀钩鍧囧垎锛屽苟缁熻楂樹簬鍜屼綆浜庡钩鍧囧垎鐨勪汉鏁帮紝鍙互浣跨敤`Scanner`绫绘潵璇诲彇杈撳叆锛岀劧鍚庣敤鏁扮粍鎴栬卄ArrayList`鏉ュ瓨鍌ㄦ垚缁╋紝鎺ョ潃杩涜璁$畻鍜岀粺璁°備互涓嬫槸涓涓畝鍗曠殑瀹炵幇渚嬪瓙锛歚``java import java.util.Scanner;public class StudentGrades { public static void main(String[]...
绛旓細public static void main(String[] args) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in));String str=br.readLine();int countNum = 0;//缁熻鏁板瓧鐨勪釜鏁 int countChar = 0;//缁熻鑻辨枃瀛楁瘝鐨勪釜鏁 int countSpace = 0;//缁熻绌烘牸鐨勪釜鏁 int ...
绛旓細}else{System.out.println("涓嶇浉绛");}}} 杩愯鏁堟灉锛歛sddsa涓嶇浉绛夐鐩2锛歩mport java.util.Scanner;/** * 棰樼洰2锛缂栫▼缁熻鐢ㄦ埛浠庨敭鐩樿緭鍏鐨勫瓧绗︿覆鎵鍖呭惈鐨勫瓧姣嶃佹暟瀛椼佸叾浠栧瓧绗︾殑涓暟 * @author Retror * */public class CountStr {public static int numCount;public static int strCount;pu...
绛旓細import java.util.Scanner;public class GetMaxMin { public static void main(String[] args) { Scanner input = new Scanner(System.in);//瀹氫箟涓涓緢闀跨殑鏁扮粍锛屽叾瀹炵敤LIST杩欓闈炲父濂藉仛锛屼笉杩囦笉鐭ラ亾浣犲杩囨病鏈 double[] numbers = new double[10000];//寰幆杈撳叆 System.out.println("璇疯緭鍏...
绛旓細import java.util.Scanner;public class InputDemo {public static void main(String[] args) {int[] ary = new int[10];//瀹氫箟涓涓竴缁存暟缁凷canner input = new Scanner(System.in);//浠庢帶鍒跺彴杈撳叆for (int i = 0; i < ary.length; i++) {System.out.println("璇疯緭鍏ョ"+(i+1)+"...
绛旓細import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Scanner;/ (1)鍒╃敤閿洏褰曞叆锛岃緭鍏ヤ竴涓瓧绗︿覆 (2)缁熻璇ュ瓧绗︿覆涓悇涓瓧绗︾殑鏁伴噺 / public class InputString { public static void main(String[] args) { //鑾峰彇閿洏杈撳叆鐨勫瓧绗...
绛旓細//package com.color.util;import java.util.Scanner;public class InputNumber { public static void main(String[] args){ int count = 0;//缁熻鏁板瓧 StringBuffer b = new StringBuffer();Scanner s = new Scanner(System.in);System.out.println("璇杈撳叆鏁板瓧锛屼互\"锛孿"闂撮殧,浠缁撴潫锛");...