计算单词中的字符数

发布于 2024-12-13 01:00:09 字数 1371 浏览 1 评论 0原文

我想创建一个程序来输出单词中字母出现的次数。基本上输出必须如下所示:

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 技术交流群。

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

发布评论

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

评论(3

病毒体 2024-12-20 01:00:09

首先,boolean 仅包含 truefalse 值,您无法使用它们进行计数。您可以简单地增加计数器,而不是在循环中将数组中映射到某个字符的每个元素设置为 true

在这种情况下,您将需要一个 int 而不是 boolean。要打印出特定字母的出现次数,显然只需打印数组的该特定索引处的 int 值即可。

另一种方法是使用 Map,而不是计算数组中特定 char 的索引。

编辑:正如戴夫提到的,你的 if 语句也被破坏了。

For a start, booleans only hold a true or false value, you can't count with them. Rather than in your loop setting each element in the array that maps to a certain character to true, you could simply increment the counter.

In this case you're going to require an int as opposed to a boolean. And to print out the amount of occurrences for a specific letter, you obviously just print out the int value at that specific index of the array.

An alternative is also a Map<Character,Integer>, rather than calculating the index for specific chars in your array.

EDIT: As Dave mentioned, your if statement is also broken.

离鸿 2024-12-20 01:00:09

如果不出意外,注释掉的 do/while 中的 if 语句使用单个 = (赋值)而不是 = =(比较)。

您真正需要做的就是为每次猜测保留一个计数,将其初始化为零,并且每次遇到猜测的字母时,将其递增。

如果您试图在猜测之前“预先”完成所有这些工作,那么您实际上并不需要布尔值映射,您需要一个整数映射,每个小写字母一个,初始化为零,并递增每个猜测其对应字母的时间。

你们非常接近。

If nothing else, that if statement in your commented-out do/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.

百合的盛世恋 2024-12-20 01:00:09

您不需要 alphabet 的布尔数组 - 它只告诉您单词中出现了哪些字母,但不告诉您每个字母的数量。试试这个:

boolean [] alphabet = new boolean [27];
for(letterNumber=0;letterNumber<lower.length();letterNumber++)
{
    ++alphabet[lower.charAt(letterNumber) - 'a'];
}

然后稍后:

System.out.printf("There are %d %s's\n",alphabet[letter.charAt(0) - 'a'], letter.charAt(0));

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:

boolean [] alphabet = new boolean [27];
for(letterNumber=0;letterNumber<lower.length();letterNumber++)
{
    ++alphabet[lower.charAt(letterNumber) - 'a'];
}

Then later:

System.out.printf("There are %d %s's\n",alphabet[letter.charAt(0) - 'a'], letter.charAt(0));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文