代码拉取完成,页面将自动刷新
//*************************************************************************************
// LetterCount.java Author: Lewis/Loftus
//
// Demonstrates the relationship between arrays an strings.
//*************************************************************************************
import java.util.Scanner;
public class LetterCount
{
//-----------------------------------------------------------------------------------
// Reads a sentence from the user and counts the number of uppercase and lowercase
// letters contained in it.
//-----------------------------------------------------------------------------------
public static void main(String[] args)
{
final int NUMCHARS = 26;
Scanner scan = new Scanner(System.in);
int[] upper = new int[NUMCHARS];
int[] lower = new int[NUMCHARS];
char current; // the current character being processed
int other = 0; // counter 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);
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。