gpt4 book ai didi

java - 打印字符串中不同字母的数量并打印最常见字母出现的次数

转载 作者:太空宇宙 更新时间:2023-11-04 09:53:52 24 4
gpt4 key购买 nike

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

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 感到困惑。我尝试过谷歌搜索并在这个和其他编程网站上查找。

我的代码是:

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 点的帮助。
  • 显示“(换行)”的地方是应该输入的位置。
  • 显示“(空)”的地方就是答案所在的地方。
  • 我知道代码末尾有一个带有(“”)的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 之前完成。

真诚的,无名先生

最佳答案

对于第一,您可以在数组中制作一个“ list ”:

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 中的 sentence):

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

关于java - 打印字符串中不同字母的数量并打印最常见字母出现的次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54384750/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com