Java编程统计用户从键盘输入的字符串中所包含的字母,数字和其他字符的个数。 不要写的太难,初学者水平! 在JAVA中,编程统计用户从键盘输入的字符串中所包含的字母、...
\u7528java\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\u4e32\u7684\u4e2a\u6570?import java.util.Scanner;public class Test { public static void main(String [] args){ Scanner input = new Scanner(System.in); System.out.print("\u8f93\u5165\u5b57\u7b26\u4e32\uff1a"); 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 = 'a' && c = 'A' && c <= 'Z' ){ chara++; }else{ other++; } } System.out.println("\u6570\u5b57\u6709:" + number + "\u4e2a\uff0c\u5b57\u7b26\u6709" + chara + "\u4e2a\uff0c\u5176\u4ed6\u6709\uff1a" + other + "\u4e2a\u3002" ); }}
\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;
/**
* 统计字符串中数字,字母,空格,其他字符的个数
* @author Administrator
*
*/
public class Data01 {
public static void main(String[] args) {
int englishCount = 0;// 英文字母个数
int spaceCount = 0;// 空格个数
int numCount = 0;// 数字个数
int otherCount = 0;// 其他字符个数
Scanner sc = new Scanner(System.in);
System.out.println("请您输入一行字符:");
String str = sc.nextLine();// 取得控制台输入的一行字符
char[] ch = str.toCharArray();// 把取道的字符串变成一个char数组
for (int i = 0; i < ch.length; i++) {
if (Character.isLetter(ch[i])) {
// 判断是否为字母
englishCount++;
} else if (Character.isSpaceChar(ch[i])) {
// 判断是否为空格
spaceCount++;
} else if (Character.isDigit(ch[i])) {
// 判断是否为数字
numCount++;
} else {
// 以上都不是则认为是其他字符
otherCount++;
}
}
System.out.println("字母的个数:" + englishCount);
System.out.println("数字的个数:" + numCount);
System.out.println("空格的个数:" + spaceCount);
System.out.println("其他字符的个数:" + otherCount);
}
}
代码如上
import java.util.Scanner; //导入Scanner类
public class Test{
public static void main (String args [ ] ){
Scanner s = new Scanner(System.in);//实例化输入流
String st = null ; //定义字符串用于接收输入
System.out.println("请输入字符串:");
st =s.next(); //完成对输入的接收
char[] c= st.toCharArray(); //将接收的字符串转化为字符数组
int n=0; //用于数字字符计数
for (int i = 0; i < c.length; i++) {
if(c[i]>='0'&&c[i]<='9'){
n++;
}
}
int n1=0; //用于小写字母字符计数
for (int i = 0; i < c.length; i++) {
if(c[i]>='a'&&c[i]<='z'){
n1++;
}
}
int n2=0; //用于大写字母字符计数
for (int i = 0; i < c.length; i++) {
if(c[i]>='A'&&c[i]<='Z'){
n2++;
}
}
int n3=c.length-n-n1-n2; //排除已计数字符后的剩余字符数
System.out.println("您输入的字符串中数字的个数是"+n);
System.out.println("您输入的字符串中小写字母的个数是"+n1);
System.out.println("您输入的字符串中大写字母的个数是"+n2);
System.out.println("您输入的字符串中其它字符的个数是"+n3);
}
}
绛旓細楠岃瘉杩囦簡锛屽彲浠ヤ竴鐩杈撳叆鎴愮哗锛岀洿鍒拌緭鍏ヤ换鎰忎竴涓礋鏁伴鍑恒俰mport java.util.Scanner;public class Statistics { private static int stuScore = 0;private static int cntFor0and9 = 0;private static int cntFor10and19 = 0;private static int cntFor20and29 = 0;private static int cntFor30and...
绛旓細import java.util.*;public abstract class test { public static void main(String[] args) { int js=0;int zs=0;for(int i=0;i<10000;i++){ Scanner in=new Scanner(System.in);System.out.print("璇杈撳叆涓涓暣鏁帮紙鐩村埌杈撳叆10000鍋滄锛:");int sz=in.nextInt();int gewei=sz%10;i...
绛旓細import java.util.Scanner;public class Test { public static void main(String[] args) { int i = 0;int sum = 0;Scanner sc = new Scanner(System.in);for(int j =1;j<=10;j++){ System.out.println("璇杈撳叆绗"+j+"涓暟锛");i = sc.nextInt();sum += i;} System.out....
绛旓細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) { //鑾峰彇閿洏杈撳叆鐨勫瓧绗...
绛旓細鎴戞槸鐢java缂栫殑浣犵湅涓涓嬶細import java.io.BufferedReader;import java.util.Scanner;//缂栧啓涓涓▼搴忥紝鍏跺姛鑳戒负锛浠庨敭鐩樿緭鍏涓琛屽瓧绗︼紝鍒嗗埆缁熻鍏朵腑瀛楁瘝銆佸瓧绗︺佺┖鏍间互鍙婂叾浠栧瓧绗︾殑涓暟 public class Test{ public static void main(String[] args){ int letter = 0;//璁板綍瀛楁瘝鐨勪釜鏁 int chars = ...
绛旓細鎴戞潵璇曡瘯锛屼笉鏄緢闅惧暒 锛屾悶瀹氱紪璇戣繃浜嗭紝鏂囦欢鍚嶄负DS.java 缂洪櫡鏄緭鍏ユ暟涓嶈兘澶ぇ路路路//缂栧啓涓涓瓧绗︾晫闈㈢殑Java Application 绋嬪簭锛屾帴鍙鐢ㄦ埛浠庨敭鐩樿緭鍏鐨勪竴涓鏁存暟锛岀劧鍚缁熻骞惰緭鍑轰粠1鍒拌繖涓鏁存暟鐨勭疮鍔犲拰銆俰mport java.awt.*;import java.awt.event.*;public class DS implements ActionListe...
绛旓細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)+"...
绛旓細package test0324;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class PrintTest { / param args / public static void main(String[] args) { InputStreamReader ir;BufferedReader in;ir = new InputStreamReader(System.in);//鍒涘缓杈撳叆娴佸璞...
绛旓細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 ...
绛旓細import java.util.Scanner;public class Test5 { public static String getInputString(int type) { Scanner scan = new Scanner(System.in);switch (type) { case 1:System.out.print("杈撳叆瀛椾覆锛");break;case 2:System.out.print("瑕佹煡鎵惧瓧涓蹭负锛");break;case 3:System.out.print("瑕...