OJ------统计大写字母个数

来源:互联网 发布:实用魔术教学软件 编辑:程序博客网 时间:2024/05/17 09:05

大写字母在‘A’----‘Z’范围内,统计输入的字符串中包含的大写字母的个数

输入样例:!@#¥A#D¥sdsasrtyt....?*7]\\

输出样例:2

关键点:

1.输出字符串包含空格和回车符(\n\r)

2.连续存在多个空格,所以不能用while(sc.hasNext()){}sc.close();

import java.util.*;public class Main{public static void main(String[] args){Scanner sc=new Scanner(System.in);//while(sc.hasNext()){String s=sc.nextLine(); //留心输入包含\n\r(回车、空格)   //charAt();char[] ch=s.toCharArray();int sum=0;if(ch.length==0||ch==null){ System.out.print(0);// break;}else{for(int i=0;i<ch.length;i++ ){if(ch[i]>='A'&&ch[i]<='Z'){    sum++;}}    System.out.print(sum);//}  //  sc.close();}}}

0 0