打印字符串中不同字母的数量并打印出现频率最高的字母的次数

时间:2019-01-27 03:11:09

标签: java loops for-loop

我确实需要学校项目的帮助。必须编写一个程序:

1: Prints the number of different letters in a string.

2: Prints the number of vowels in a string.

3: Prints the number of uppercase letters in a string.

4: Prints the number of times that the most frequent letter appears in a string.

5: Prints the longest word in the string.

目前我已经完成了数字2、3和5。我真的对1和4感到困惑。我曾尝试使用google搜索并在此网站和其他编程网站上查找。

我的代码是:

import java.util.Scanner;
/**
 * Write a description of class Practice_2 here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Practice_2
{
    // instance variables - replace the example below with your own
    private int x;

    /**
     * Constructor for objects of class Practice_2
     */
    public Practice_2()
    {
        // initialise instance variables
        x = 0;
    }

    /**
     * An example of a method - replace this comment with your own
     *
     * @param  y  a sample parameter for a method
     * @return    the sum of x and y
     */
    public int sampleMethod(int y)
    {
        // put your code here
        return x + y;
    }

    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        String sentence = scan.nextLine();
        String[] sen = sentence.split("\\s");
        int numVowels = 0;
        int numUpper = 0;
        int [] alphaarray = new int[26];
        int longest = 0;
        for(int i = 0; i < sen.length; i ++)
        {
            char c = sen[i].charAt(sen[i].length()-1);
            if(sen[i].length() > sen[longest].length())
            {
                longest = i;
            }
            if(c == ',' && sen[i].length()-1 > sen[longest].length())
            {
                longest = i;
            }
        }
        for(int i = 0; i < sentence.length(); i++)
        {
            char c = sentence.charAt(i);
            if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U')
            {
                numVowels++;
            }
            if(Character.isUpperCase(c))
            {
                numUpper++;
            }
            if (c == 'a') alphaarray[0]++;
            if (c == 'b') alphaarray[1]++;
        }



        System.out.println(" ");
        System.out.println(numVowels);
        System.out.println(numUpper);
        System.out.println(" ");
        System.out.println(sen[longest]);
    }

}

我真的需要完成这个项目。我正在用Java处理这个问题,并使用程序BlueJ来做到这一点。

说明:

  • 程序编号1应该打印一个1到26之间的数字,以告诉您有多少个不同的字母。
  • 我真的不希望更改已有的任何代码,因为它可以正常工作。
  • 我只需要1号和4号帮助。
  • 应在其中输入“(新行)”的地方。
  • 答案应该在哪里“(Empty)”。
  • 我知道在具有(“”)的代码的末尾有一个System.out.println。这就是答案1和4的变量所在的位置。
  • 句子最长为1024个字母。
  • 我知道有this个帖子存在,但是当我尝试使用它时,我无法使它正常工作。
  • 我也知道this帖子也存在,但是它是用C编写的,我需要用Java编写。

样本输入为:

The quick Brown fox, named Roxanne, jumped over Bruno, the lazy dog.

此输出为: (空) 19(新行) 3(新行) (空) 罗克珊

我需要这样:

25(新行) 19(新行) 3(新行) 6(新行) 罗克珊

这必须通过循环并在main(String[] args)方法内部完成。

我知道我在晚上发布此帖子时已经很晚了,但是可以帮助您,因为需要在周日的2:15之前完成。

此致, MrNoName

1 个答案:

答案 0 :(得分:0)

对于第一名,您可以在数组中创建一个“清单”:

String sentencel=sentence.toLowerCase();
char[] letters;
letters=new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
int numDiffLetters=0;
for(int i=0; i<sentencel.length(); i++){
    for(int j=0; j<letters.length; j++){
        if(sentencel.charAt(i)==letters[j]){
            letters[j]='0';
            break;
        }
    }
}
for(int i=0; i<letters.length; i++){
    if(letters[i]=='0'){
        numDiffLetters++;
    }
}

然后打印numDiffLetters。 对于第四个数字,您可以找到每个数字出现多少次,然后找到最大的数字(您需要从第1个数字开始sentencel):

char[] letterarray;
letterarray=new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
int[] letterTimes;
letterTimes=new int[26];
int numMostLetter=0;
for(int i=0; i<sentencel.length(); i++){
    for(int j=0; j<letterarray.length; j++){
        if(sentencel.charAt(i)==letterarray[j]){
            letterTimes[j]++;
            break;
        }
    }
}
Arrays.sort(letterTimes);
numMostLetter=letterTimes[letterTimes.length-1];

然后打印numMostLetter