2 Star 0 Fork 0

CS-IMIS-23/20172309_javaProgramming

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
eg8_3.java 1.88 KB
一键复制 编辑 原始数据 按行查看 历史
20172309 提交于 7年前 . 例子8.3 LetterCount
//***************************************************************************
//LetterCount.java Author:WZW
//
//Denonstrates the relationship between array an strings.
//***************************************************************************
import java.util.Scanner;
public class eg8_3 {
//-----------------------------------------------------------------------
//Resds a sentence from thr user and counts the number of
//uppercase and lowercase letters contained in it.
//-----------------------------------------------------------------------
public static void main(String[] args) {
final int MUNCHARS=26;
Scanner scan =new Scanner(System.in);
int [] upper=new int [ MUNCHARS];
int []lower= new int [MUNCHARS];
char current;//the current character being processing.
int other =0;//Count for non-alphabetics.
System.out.println("Enter a sentence: ");
String line =scan.nextLine();
//Count the number of each letter occurrence.
for (int ch =0;ch <line.length();ch++){
{
current = line.charAt(ch);
if (current >= 'A' && current <= 'Z')
upper[current - 'A']++;
else if (current >= 'a' && current <= 'z')
lower[current - 'a']++;
else
other++;
}
//Print the results
System.out.println();
for (int letter=0;letter<upper.length;letter++)
{
System.out.print((char)(letter+'A'));
System.out.print(":"+upper[letter]);
System.out.print("\t\t"+(char)(letter+'a'));
System.out.println(": "+lower[letter]);
}
System.out.println();
System.out.println("Non-alphabetic characters: "+other);
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/CS-IMIS-23/20172309_javaProgramming.git
git@gitee.com:CS-IMIS-23/20172309_javaProgramming.git
CS-IMIS-23
20172309_javaProgramming
20172309_javaProgramming
master

搜索帮助