这个计划进展如何?

发布于 2024-12-15 05:23:41 字数 1149 浏览 0 评论 0原文

指示:

编写一个程序来读取一行结尾的文本 带有句点,用作哨兵值。显示所有 文本中出现的字母,每行一个,按字母顺序排列 顺序以及每个字母在文本中出现的次数。 使用长度为 26 的基类型 int 数组,以便位于 索引 0 包含 a 的数量。索引 1 包含 b 等的数量。

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);
    }
}
}

仍然有句号问题。它似乎不像我那样工作。

Instructions:

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);
    }
}
}

Still having problem with the period thing.. it does not seem to work the way i did it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

掩于岁月 2024-12-22 05:23:41

考虑到这是一项作业并且说明非常具体,您应该逐个字符地阅读文本,而不是使用内置函数

如果您的文本文件类似于

abcabca.

输出应该是a出现三次,b出现两次等等。

所以你的算法应该类似于

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

Considering this is a homework and instructions are very specific, you should read the text character by character instead of using built-in functions

If your text file was something like

abcabca.

The output should be something a appears three times, b appears two times etc etc.

So your algo should be something like

  1. Read next character
  2. If char is period goto 5
  3. If char is space goto 1.
  4. If char is between a <-> z. update the counter in arr[0..25] and goto 1
  5. output arr[0..25] one per line
未蓝澄海的烟 2024-12-22 05:23:41

是否强制要求该作业必须用 Java 完成? “哨兵角色”的整个想法而不仅仅是使用行终止符是相当奇怪的。

无论如何,您可以通过设置 Scanner 的分隔符来实现您想要的行为:

keyboard.useDelimiter("\\.");

至于循环,一个重要提示是:

int[] counts;
counts[chars[0] - 'a'] = counts[chars[0] - 'a'] + 1;

或者

counts[chars[0] - 'a']++;

我将由您将其包含在循环中。

编辑

如果您正在寻找一次字符输入,我建议您使用 InputStreamReader 而不是 Scanner 进行输入。下面是它的基本框架:

Reader reader = new InputStreamReader(System.in);
while (true) {
    int nextInput = reader.read();

    if (nextInput == -1) {
       System.out.println("End of input reached without sentinal character");
       break;
    } 
    char nextChar = (char) nextInput;
    //deal with next character

}

尽管如此,read() 通常会阻塞,直到到达输入末尾(CTRL-DCTRL-Z(大多数控制台)或发送新行。因此,符号字符的用途有限,因为在输入“.”后您仍然需要执行某些操作。

Was it mandated that this assignment is done in Java? The whole idea of a "sentinal character" rather than just using a line terminator is pretty bizarre.

Anyway, you can achieve the behaviour you want by setting the delimiter of Scanner:

keyboard.useDelimiter("\\.");

As for the looping, a big hint is this:

int[] counts;
counts[chars[0] - 'a'] = counts[chars[0] - 'a'] + 1;

or simply

counts[chars[0] - 'a']++;

I'll leave it up to you to include that in a loop.

Edit

If you are looking for character-at-a-time input, I would suggest you use an InputStreamReader instead of Scanner for your input. Here's a basic skeleton of what that looks like:

Reader reader = new InputStreamReader(System.in);
while (true) {
    int nextInput = reader.read();

    if (nextInput == -1) {
       System.out.println("End of input reached without sentinal character");
       break;
    } 
    char nextChar = (char) nextInput;
    //deal with next character

}

Still, read() will typically block until either the end of input is reached (CTRL-D or CTRL-Z from most consoles) or a new line is sent. Thus the sentinal character is of limited use since you still have to do something after typing ".".

高冷爸爸 2024-12-22 05:23:41
  1. 您必须检查末尾是否有句号。所以最后一个字符应该是“.”。
  2. 然后获取最后一个“.”之前的字符串长度。
  3. 对于计数部分,创建一个像您正在做的数组:
    int[] 名称 = 新 int[26]
    其中从 0、25 开始的每个索引对应于“a”到“z”。

现在,您将字符串字符放入循环中,并且必须检查该字符是什么样的:

if its a 'a' : increase the value at index 0 by 1. 
if its a 'd' : increase the value at index 3 by 1. 

同样。

稍后您将显示整个数组,其中包含 a、z 以及从 0 到 25 的索引。

建议:如果不需要使用数组,并且您可以使用任何其他数据结构,则可以在 HashMap< 中实现相同的数据结构/strong> 非常容易。将 'a', 'z' 保留为键并计数为相应的值。然后检索和显示值也会更容易。

  1. You have to check whether period is there at the end or not. So the last character should be '.'.
  2. Then take the length of string before last '.'.
  3. For the counting part create an array like u are doing :
    int [] name = new int[26]
    where each index starting from 0, 25 corresponds to 'a' till 'z'.

Now you put the string characters in a loop and have to check what that character is like :

if its a 'a' : increase the value at index 0 by 1. 
if its a 'd' : increase the value at index 3 by 1. 

like wise.

later you display the whole array with a, z along with indexes from 0 till 25.

Suggestion: If its not required to use an array, and you can use any other data-structure you can implement the same in a HashMap very easily. by keeping 'a', 'z' as the keys and count as the corresponding values. and then retrieving and showing the values will also be easier.

四叶草在未来唯美盛开 2024-12-22 05:23:41

您需要一个 int 数组(例如 int[] counts = new int[26];) 读取输入行后,在循环中逐个字符地检查它。如果字符不是句点,则增加计数数组的相应元素。 (如果字符是a,则增加counts[0];如果是b,则增加counts[1]提示:您可以从字符中减去 a 以获得适当的索引。)当您找到句点时,退出循环并打印结果(可能使用第二个循环)。

You need an int array (e.g., int[] counts = new int[26];) After you read the input line, examine it character by character in a loop. If the character is a not period, then increment the appropriate element of the counts array. (If the character is a, then increment counts[0]; if it is b, increment counts[1]; etc. Hint: you can subtract a from the character to get the appropriate index.) When you find a period, exit the loop and print the results (probably using a second loop).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文