第五章第五十题(对大写字母计数)(count the uppercase letters)

*5.50(对大写字母计数)编写一个程序,提示用户输入一个字符串,然后显示该字符串大写字母的数目。

Enter a string: Welcome to Java
The number of uppercase letters is 2

*5.50 (count the uppercase letters) Write a program that prompts the user to enter a string,And displays the number of uppercase letters in the string.

Enter a string: Welcome to Java
The number of uppercase letters is 2

下面是参考答案代码:

import java.util.*;

public class CountUpperCaseletterQuestion50 {
	public static void main(String[] args) {
		String userString;
		int upperLetterCount = 0;
		
		Scanner inputScanner = new Scanner(System.in);
		System.out.print("Enter a string: ");
		userString = inputScanner.nextLine();
		
		for(int strIndex = 0;strIndex < userString.length();strIndex++)
			if(userString.charAt(strIndex) >= 65 && userString.charAt(strIndex) <= 90)
				upperLetterCount++;
		System.out.printf("The number of uppercase letters is %d", upperLetterCount);
		
		inputScanner.close();
	}
}

运行效果:
在这里插入图片描述

注:编写程序要养成良好习惯
1.文件名要用英文,具体一点
2.注释要英文
3.变量命名要具体,不要抽象(如:a,b,c等等),形式要驼峰化
4.整体书写风格要统一(不要这里是驼峰,那里是下划线,这里的逻辑段落空三行,那里相同的逻辑段落空5行等等)
5.普通变量,方法名要小驼峰,类名要大驼峰,常量要使用全部大写加上下划线命名法
6.要学习相应的代码编辑器的一些常用快捷键,如:快速对齐等等

发布了139 篇原创文章 · 获赞 1 · 访问量 4628

猜你喜欢

转载自blog.csdn.net/xjlovewjh/article/details/104315384