说明:
Write a program that will read a line of text that ends with a period, which serves as a sentinel value. Display all the letters that occur in the text, one per line and in alphabetical order, along with the number of times each letter occurs in the text. Use an array of base type int of length 26 so that the element at index 0 contains the number of
a
s. and index 1 contain number ofb
s etc.
package alphabetize;
import java.util.*;
public class Alphabetize
{
private static void number(String s)
{
int[] array = new int[26];
s = s.toUpperCase();
System.out.println(s);
for (int i = 0; i < s.length(); ++i)
{
if (s.charAt(i) >= 'A' && s.charAt(i) <= 'Z')
{
++array[s.charAt(i) - 'A'];
}
}
for (int i = 0; i < 26; ++i)
{
System.out.println("|" + (char) ('A' + i) + "|" + array[i] + "|");
}
}
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
String aString = ".";
while (true)
{
System.out.println("Please enter sentence with a period to end");
aString = keyboard.nextLine();
if (".".equals(aString))
{
System.exit(0);
}
number(aString);
}
}
}
关于经期的事情仍然有问题..它似乎不像我那样工作。
最佳答案
考虑到这是一项作业并且说明非常具体,您应该逐字阅读文本,而不是使用内置函数
如果你的文本文件是这样的
abcabca.
输出应该是 a 出现三次,b 出现两次等等。
所以你的算法应该是这样的
- 读下一个字符
- 如果char是句点goto 5
- 如果 char 是空格转到 1。
- 如果 char 在 <-> z 之间。更新 arr[0..25] 中的计数器并转到 1
- 每行输出一个arr[0..25]
关于java - 这个项目进展如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8117352/