计算单词中的字符数
我想创建一个程序来输出单词中字母出现的次数。基本上输出必须如下所示:
Please enter a word—howdy
The word howdy has five characters.
What letter would you like to guess? (enter zero to quit) a
There are 0 A’s.
What letter would you like to guess? (enter zero to quit) b
There are 0 B’s.
这是我到目前为止得到的:
import java.util.Scanner;
public class LetterOccurence
{
public static void main(String[]args)
{
Scanner input=new Scanner(System.in);
String word;
String letter;
int letterNumber;
boolean [] alphabet = new boolean [27];
System.out.println("Please enter a word- ");
word=input.next();
System.out.println("The word "+ word + " has " + word.length() + " letters\n");
String lower = word.toLowerCase();
for(letterNumber=0;letterNumber<lower.length();letterNumber++)
{
alphabet[lower.charAt(letterNumber) - 'a'] = true;
}
//do
//{
System.out.println("What letter would you like to guess? (enter 0 to quit) ");
letter = input.next();
if (alphabet[letter.charAt(0)-'a'] = true)
{
System.out.printf("There are %s's\n",letter.charAt(0));
}
else
{
System.out.printf("There are 0 %s's\n",letter.charAt(0));
}
//}while(Character.isLetter(letter.charAt(0)));
}
}
我现在陷入困境,关于如何继续的任何想法?
I want to create a program to output the number of occurrences of a letter in a word. Basicaly the output has to look like this:
Please enter a word—howdy
The word howdy has five characters.
What letter would you like to guess? (enter zero to quit) a
There are 0 A’s.
What letter would you like to guess? (enter zero to quit) b
There are 0 B’s.
This is what I got so far:
import java.util.Scanner;
public class LetterOccurence
{
public static void main(String[]args)
{
Scanner input=new Scanner(System.in);
String word;
String letter;
int letterNumber;
boolean [] alphabet = new boolean [27];
System.out.println("Please enter a word- ");
word=input.next();
System.out.println("The word "+ word + " has " + word.length() + " letters\n");
String lower = word.toLowerCase();
for(letterNumber=0;letterNumber<lower.length();letterNumber++)
{
alphabet[lower.charAt(letterNumber) - 'a'] = true;
}
//do
//{
System.out.println("What letter would you like to guess? (enter 0 to quit) ");
letter = input.next();
if (alphabet[letter.charAt(0)-'a'] = true)
{
System.out.printf("There are %s's\n",letter.charAt(0));
}
else
{
System.out.printf("There are 0 %s's\n",letter.charAt(0));
}
//}while(Character.isLetter(letter.charAt(0)));
}
}
I am now stuck, any ideas on how to continue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,
boolean
仅包含true
或false
值,您无法使用它们进行计数。您可以简单地增加计数器,而不是在循环中将数组中映射到某个字符的每个元素设置为true
。在这种情况下,您将需要一个
int
而不是boolean
。要打印出特定字母的出现次数,显然只需打印数组的该特定索引处的int
值即可。另一种方法是使用
Map
,而不是计算数组中特定char
的索引。编辑:正如戴夫提到的,你的
if
语句也被破坏了。For a start,
boolean
s only hold atrue
orfalse
value, you can't count with them. Rather than in your loop setting each element in the array that maps to a certain character totrue
, you could simply increment the counter.In this case you're going to require an
int
as opposed to aboolean
. And to print out the amount of occurrences for a specific letter, you obviously just print out theint
value at that specific index of the array.An alternative is also a
Map<Character,Integer>
, rather than calculating the index for specificchar
s in your array.EDIT: As Dave mentioned, your
if
statement is also broken.如果不出意外,注释掉的
do/while
中的if
语句使用单个=
(赋值)而不是= =
(比较)。您真正需要做的就是为每次猜测保留一个计数,将其初始化为零,并且每次遇到猜测的字母时,将其递增。
如果您试图在猜测之前“预先”完成所有这些工作,那么您实际上并不需要布尔值映射,您需要一个整数映射,每个小写字母一个,初始化为零,并递增每个猜测其对应字母的时间。
你们非常接近。
If nothing else, that
if
statement in your commented-outdo/while
is using a single=
(assignment) instead of==
(comparison).All you really need to do is keep a single count per guess, initialize it to zero, and each time you encounter the guessed letter, increment it.
If you're trying to do all that work "up front" before guessing, then you don't really need a map of booleans, you need a map of ints, one per lower-case letter, initialized to zero, and incremented each time its corresponding letter is guessed.
You're very close.
您不需要
alphabet
的布尔数组 - 它只告诉您单词中出现了哪些字母,但不告诉您每个字母的数量。试试这个:然后稍后:
You don't want a boolean array for
alphabet
-- that only tells you what letters appear in the word, but not how many of each. Try this instead:Then later: