这个计划进展如何?
指示:
编写一个程序来读取一行结尾的文本 带有句点,用作哨兵值。显示所有 文本中出现的字母,每行一个,按字母顺序排列 顺序以及每个字母在文本中出现的次数。 使用长度为 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 ofa
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);
}
}
}
Still having problem with the period thing.. it does not seem to work the way i did it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
考虑到这是一项作业并且说明非常具体,您应该逐个字符地阅读文本,而不是使用内置函数
如果您的文本文件类似于
输出应该是a出现三次,b出现两次等等。
所以你的算法应该类似于
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
The output should be something a appears three times, b appears two times etc etc.
So your algo should be something like
是否强制要求该作业必须用 Java 完成? “哨兵角色”的整个想法而不仅仅是使用行终止符是相当奇怪的。
无论如何,您可以通过设置 Scanner 的分隔符来实现您想要的行为:
至于循环,一个重要提示是:
或者
我将由您将其包含在循环中。
编辑
如果您正在寻找一次字符输入,我建议您使用
InputStreamReader
而不是Scanner
进行输入。下面是它的基本框架:尽管如此,
read()
通常会阻塞,直到到达输入末尾(CTRL-D 或 CTRL-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
:As for the looping, a big hint is this:
or simply
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 ofScanner
for your input. Here's a basic skeleton of what that looks like: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 ".".int[] 名称 = 新 int[26]
其中从 0、25 开始的每个索引对应于“a”到“z”。
现在,您将字符串字符放入循环中,并且必须检查该字符是什么样的:
同样。
稍后您将显示整个数组,其中包含 a、z 以及从 0 到 25 的索引。
建议:如果不需要使用数组,并且您可以使用任何其他数据结构,则可以在 HashMap< 中实现相同的数据结构/strong> 非常容易。将 'a', 'z' 保留为键并计数为相应的值。然后检索和显示值也会更容易。
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 :
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.
您需要一个 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 isa
, then incrementcounts[0]
; if it isb
, incrementcounts[1]
; etc. Hint: you can subtracta
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).