首页 新闻 会员 周边 捐助

请问结果是?

-3
悬赏园豆:15 [待解决问题]

阅读下列程序,写出程序运行的结果:

importjava.util.Scanner;
public class TestCharcount1{
public static void main(String []args){
int strNum=0;
int numNum=0;
int spcNum=0;
Scanner scanner=new Scanner(System.in);
System.out.print("请输入字符串:");
String str=scanner.nextLine(;
Character|]c=new Character[str.length0];
for(int i=0;i<str.length(;i++){
c[i]=str.charAt(1);
for(char j='0';j<='9';j++)
if(c[i]==j)
numNum++;
for(char j='a';j<='z';j+ +)
if(c[i].equals()))
strNum++;
for(char j='A';j<='Z';j++)
if(c[i].equals(j))
strNum++;
if(c[i].equals(''))
spcNum++;
}
System.out.println("strNum="+strNum);
System.out.printin("numNum="+numNum);
System.out.println("spcNum ="+spcNum);
/
}
输入字符串: I'm 22 years old in this year.

程序运行的结果:
(1)_______
(2)_____
(3)_________

peir的主页 peir | 初学一级 | 园豆:105
提问于:2022-12-15 23:50
< >
分享
所有回答(1)
0

import java.util.Scanner;

public class TestCharcount1 {
public static void main(String []args) {
int strNum = 0;
int numNum = 0;
int spcNum = 0;

    Scanner scanner = new Scanner(System.in);
    System.out.print("请输入字符串:");
    String str = scanner.nextLine();
    Character[] c = new Character[str.length()];
    
    for(int i = 0; i < str.length(); i++) {
        c[i] = str.charAt(i);
        
        for(char j = '0'; j <= '9'; j++)
            if(c[i] == j)
                numNum++;
                
        for(char j = 'a'; j <= 'z'; j++)
            if(c[i] == j)
                strNum++;
                
        for(char j = 'A'; j <= 'Z'; j++)
            if(c[i] == j)
                strNum++;
                
        if(Character.isWhitespace(c[i]))
            spcNum++;
    }
    
    System.out.println("strNum=" + strNum);
    System.out.println("numNum=" + numNum);
    System.out.println("spcNum =" + spcNum);
}

}
输出:
strNum=21
numNum=2
spcNum=6

npe0 | 园豆:1502 (小虾三级) | 2023-12-12 17:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册