【问题标题】:how many letter occur in a text?一个文本中有多少个字母?
【发布时间】:2014-11-23 19:46:30
【问题描述】:

我让它工作了,但它也在计算正在输入的单词之间的空格。 我只关心字母而不是空格。 我该怎么做才能改变这种情况?我在哪里搞砸或错过了什么

import java.util.Scanner;

public class LetterCount
{
public static void main (String[] args)
{

    System.out.println("Type something ");
    Scanner n = new Scanner (System.in);
    String s = "";
    s = n.nextLine();
    char []c = s.toCharArray();
    int sz = c.length;
    int i =0, j=0, counter=0;

    for (i=0; i<sz; i++){
        counter =0;

            for(j=0; j<sz; j++)
            {

                if (j< i&& c[i] == c[j])
                {
                    break;
                }   

                if (c[j] == c[i])
                {
                    counter++;
                }

                if (j==sz-1)
                {
                    System.out.println("the character "+c[i]+" is present "+counter+" times");
                }
            }
    }


}

}

【问题讨论】:

  • 请先尝试一下,然后再向我们展示。
  • 导入 java.util.Scanner; public class LetterCount { public static void main (String[] args) { System.out.println("Type something");扫描仪 n = 新扫描仪(System.in);字符串 s = ""; s = n.nextLane(); // 告诉找不到符号 char []c = s.toCharArray(); int sz = c.length; int i =0,j=0,计数器=0; for (i=0; i

标签: java arrays for-loop java.util.scanner


【解决方案1】:

您要做的是在计算字母之前从字符串中删除空格;

Removing whitespace from strings in Java

【讨论】:

    猜你喜欢
    • 2019-11-03
    • 2014-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-21
    • 1970-01-01
    • 2019-04-21
    • 2017-05-24
    相关资源
    最近更新 更多