- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我真的需要学校项目的帮助。必须编写一个程序:
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 程序来完成此操作。
澄清:
示例输入是:
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/
新建表: create table [表名] ( [自动编号字段] int IDENTITY (1,1)&nbs
我的文件中有正在本地化的字符串。其中许多是常见的,并且已经在整个 iOS 中使用。例如。 “保存”、“加载”、“返回”、“收藏夹”、“拍照”。为了与其他应用程序和内置应用程序提供一致的用户体验,是否有
我已经学习了 Qt 的基础知识,现在对这个漂亮的库的深度感兴趣。请帮助我理解: 所有类都是从QObject派生的吗? 为什么可以在QWidget(和派生类)上绘画? return app.exec()
我在 webpack 中设置了一个自调用函数,并使用常见的 JS 来需要一些包: (function() { var $ = require("jquery"); //...my functi
我正在尝试制作一个大量使用词性标记的应用程序。但是 nltk 的 pos 标记功能对我来说似乎不符合标准 - 例如: import nltk text = "Obama delivers his fi
有没有办法处理发送到 MySQL 的常见查询以防止不必要的带宽使用? 最佳答案 选项是: 使用MySQL缓存查询 好:全自动 差:仍然需要访问数据库服务器;有一次缓存让我在一个项目中失望,花了很长时间
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 4 年前。 Improve this qu
关闭。这个问题需要debugging details .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 6年前关闭。 Improve this questio
我正在尝试调用返回 csv 文件的网络服务。因此,我调用的每个 URL 都有一个后缀,它是一个字符串,表示要生成哪个 csv。然后我想将此 csv 保存到文件中。有很多要生成,所以我从多个线程调用此类
流行手机型号支持的典型触摸点数量是多少?我在基础研究中看到低至 2 和高至 5,但我希望能够将其映射到实际手机和更好的限制! 最佳答案 两部手机的触控点数据: Galaxy S 5 LG
出于好奇 - 我知道有 LAMP - Linux、Apache、MySQL 和 PHP。但是还有哪些其他 Web 堆栈替代方案的缩写呢?像 LAMR - Linux、Apache、MySQL Ruby
我写了一个java代码(使用apache common vfs2)来上传文件到SFTP服务器。最近,我在我的服务器上引入了 PGP 安全性。现在,java 代码无法连接到该服务器。与 FileZill
由于 GLU 被认为对于现代 OpenGL (3.1+) 来说已经过时,那么使用 C/C++ 在 OpenGL 中绘制基本形状(例如椭圆或弧线/饼图)的方法是什么?令人难以置信的是,在 OpenGL
我想知道是否有最流行的 iOS 应用程序的自定义 URL 方案列表,例如 Keynote、Numbers、Pages、Evernote 等。我还想知道这些应用程序使用什么参数网址。 我需要这个的原因是
我正在使用 NDK r10d 移植 C++ myToll Linux 应用程序以在 Android 上运行。 (请注意,这不是带有 apk 的 Android 应用程序,而是从 shell 运行的实用
假设您想要使用 UML 2 部署图为在该领域没有太多知识的人可视化一个常见的 PHP 服务器应用程序。这样一个通用的应用程序可能有三个设备节点(数据库服务器、Web 服务器和客户端)和四个执行环境节点
我正在尝试运行以下代码,以找到两个人之间的共同 friend 。输入如下 A : B C D B : A C D E C : A B D E D : A B C E E : B C D 我无法在输出文
我在 Gitolite 的 manual 中找到的唯一东西在钩子(Hook)上,是: If you want to add your own hook, it's easy as long as it
具体来说,我有一个问题,在 AWS 环境中组织 AZ 故障转移的推荐方法是什么。此外,最好了解典型的 AWS 故障以组织应用程序 HA(高可用性)。 因此,应用程序架构(AWS 服务使用)如下: 它或
我正在尝试编写一个通用的 SecurePagingAndSorting 存储库,它将检查 CRUD 操作的安全性,以节省在所有 JPA 存储库中重复相同的 PreAuthorize(使用不同的权限)。
我是一名优秀的程序员,十分优秀!