java - 这个项目进展如何?

标签 java arrays string sorting

说明:

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 as. and index 1 contain number of bs 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 出现两次等等。

所以你的算法应该是这样的

  1. 读下一个字符
  2. 如果char是句点goto 5
  3. 如果 char 是空格转到 1。
  4. 如果 char 在 <-> z 之间。更新 arr[0..25] 中的计数器并转到 1
  5. 每行输出一个arr[0..25]

关于java - 这个项目进展如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8117352/

相关文章:

SQL:分解数组

Python:将 CSV 数据存储在列表或数组中

java - 排序 ListView 时不调用比较方法

java - 如何存储和搜索 'Banned Passwords'列表

c - C 中指针递增的方法有很多种,有什么区别?

python - 返回第一个下划线之前的所有字符

c - 如何从由 iso_c_binding 的 Fortran 调用的 C 函数接收字符串?

c - 变量 C String string.h 用法

java - 在单元测试中使用多少个线程?

java - Selenium 如何单击列表中的每个相同按钮并提交表单