编写java程序:输入一个字符串,判断有几个英文字母,有几个数字,有几个其它的字符

Java编程: 用户输入一个字符串,判断有几个英文字母,有几个数字,有几个其它的字符。~

初始化三个为0的变量 分别代表字母、数字、其他字符的数量 循环遍历每个字符串中的字符 依次判断 字母的话字母那个变量++ 。。。其他同理

importjava.util.Scanner;publicclassA04{publicstaticvoidmain(Stringargs[]){System.out.println("请输入字符串:");Strings=newScanner(System.in).next();System.out.println("输出结果:");for(charc:s.toCharArray()){if(c>='a'&&c='A'&&c<='Z'){c+=32;}System.out.print(c);}}}

public class Main {
public static void main(String args[]){
String str1="abfdTE1879!!";//可以从控制台输入
String str2=str1.replaceAll("[a-z|A-Z]","");
System.out.println("英文字符的个数为"+(str1.length()-str2.length()));
str1=str2;
str2=str1.replaceAll("[0-9]","");
System.out.println("数字字符的个数为"+(str1.length()-str2.length()));
System.out.println("其它字符的个数为"+str2.length());
}
}
这是最简洁的
运行示例:
英文字符的个数为6
数字字符的个数为4
其它字符的个数为2

public static void main(String[] args) {

int count_abc=0,count_num=0,count_oth=0;
//输入一串数
Scanner scan=new Scanner(System.in);
String str = scan.next();
char[] chars = str.toCharArray();
//判断每个字符
for(int i = 0;i<chars.length;i++){
if((chars[i]>=65&&chars[i]<=90)||(chars[i]>=97&&chars[i]<=122)){
count_abc++;
}else if(chars[i]>=48&&chars[i]<=57){
count_num++;
}else{
count_oth++;
}
}
System.out.println("字母有:"+count_abc+"个");
System.out.println("数字有:"+count_num+"个");
System.out.println("其他的有:"+count_oth+"个");
}

public static void main(String[] args) {
// TODO Auto-generated method stub
int abcCount=0;//英文字母个数
int spaceCount=0;//空格键个数
int numCount=0;//数字个数
int otherCount=0;//其他字符个数
Scanner scan=new Scanner(System.in);
String str=scan.nextLine();
char[] ch = str.toCharArray();
for(int i=0;i<ch.length;i++){
if(Character.isLetter(ch[i])){
//判断是否字母
abcCount++;
}
else if(Character.isDigit(ch[i])){
//判断是否数字
numCount++;
}
else if(Character.isSpaceChar(ch[i])){
//判断是否空格键
spaceCount++;
}
else{
//以上都不是则认为是其他字符
otherCount++;
}
}
System.out.println("字母个数:"+abcCount);
System.out.println("数字个数:"+numCount);
System.out.println("空格个数:"+spaceCount);
System.out.println("其他字符个数:"+otherCount);
}

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
String getMess=scan.nextLine();
int zm=0;
int num=0;
int other=0;
char [] ch = getMess.toCharArray();
for(int i=0;i<ch.length;i++){
if((ch[i]>='a' && ch[i]<='z') || (ch[i]>='A' && ch[i]<='Z')){
zm=zm+1;
}else if(ch[i]>47 && ch[i]<58){
num=num+1;
}else{
other=other+1;
}
}
System.out.println("字母:"+zm);
System.out.println("数字:"+num);
System.out.println("其它:"+other);
}

java中怎样从键盘输入一个字符然后输出,求完整的程序
答:{ while (true){ try//---用io库就一定要加上异常处理 { BufferedReader br = new BufferedReader(new InputStreamReader(System.in));System.out.println("请输入一个字符串:");string s = br.readLine();//readLine()读入程序中的是一个字符串。//java貌似从控制台读入的都是字符串,一般...

用Java编写程序,从键盘上输入一个字符串和一个指定字符,把字符串中所...
答:过了很久才看到你的提问,不好意思,希望对你有帮助:import java.util.Scanner;public class IoTest { public static void main(String[] args) { Scanner input = new Scanner(System.in);String str1 = input.nextLine();//输入一个字符串 String str2 = input.nextLine();//指定字符 String...

java编写程序,从键盘输入任一个字符串,求字符串中有几个数字字符?(数 ...
答:public class Test { public static void main(String args[]){ Scanner scanner = new Scanner(System.in); String str = scanner.next(); //去除所有的非数字字符 String digits = str.replaceAll("[^0-9]",""); //判断去除之后是否为空 if (!digits.isEmpty())...

在java中如何输入一个char型字符。
答:可以创建Scanner类来从键盘输入一个字符,用String类型来接收,再使用String的charAt功能,具体步骤如下:1、先创建一个Scanner对象,如:Scanner sc = new Scanner(System.in);2、然后再调用Scanner对象sc的next()方法获取控制台输入,定义一个String类型的变量s来接收控制台输入的字符,如:String s = ...

Java编程,从键盘上输入一个字符串和子串的开始位置与长度,截取该字符串...
答:public class Java6 {public static void main(String[] args) {Scanner sc=new Scanner(System.in);System.out.println("请输入一个字符串:");String str=sc.nextLine();System.out.println("请输入截取的开始位置:");int a = sc.nextInt();System.out.println("请输入截取长度:");int ...

java中如何输入一个字符?
答:你可以先读入一个字符串,然后取一个字符。如果要多次读入一个字符,可以用一个循环依次读取。例如:package konw.test1;import java.util.Scanner;public class InputTest{ public static void main(String[] args) { Scanner input = new Scanner(System.in); char c; String s = ...

用java编程:任意输入一个字符串,统计其中英文字母a和i的个数,并将该...
答:import java.util.*;public class tongji { public static void main(String[] args){ String A;char[] B = new char[30];int a=0;int i=0;int l;Scanner input = new Scanner(System.in);System.out.println("输入一串字符:");A=input.next();l=A.length();for(int k=0;k<l;...

java编写程序,读入用户输入的一个字符串,然后确定并输出每一个小写元音...
答://用于保存元音字母的个数(不区分大小写)for (int i = 0; i < str.length(); i++) {char c = str.charAt(i);for (int j = 0; j < cs.length; j++) {if(c==cs[j]){arys[j]++;//小写元音个数增加sumx++;break;}}for (int j = 0; j < bcs.length; j++) {if(c...

在java中怎样在终端输入一个字符串、字符、数字。
答:可以通过”Scanner“函数 直接输入参数的形式,来实现输入语句,举例:import java.util.Scanner;public class Test { public static void main(String[] args) { Scanner input=new Scanner(System.in);System.out.println("请输入一个整数:");int length=input.nextInt();//输入一个整数 System....

Java编写一个有两个文本框和一个按钮的应用程序在一个文本框输入一个...
答:这段应该符合你的要求 import java.awt.BorderLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTextField;import sun.io.ByteToCharConverter;import sun.io.CharToByte...

IT评价网,数码产品家用电器电子设备等点评来自于网友使用感受交流,不对其内容作任何保证

联系反馈
Copyright© IT评价网