【发布时间】:2026-01-06 01:00:02
【问题描述】:
我是一名学生,对 Java 还很陌生。对于我的家庭作业,我必须:
-
要求用户使用 do while 循环输入一个数字(至少 7 个)。
-
使用 for 循环,我需要要求用户输入
that字数。 -
然后我必须检查其中一个词是否满足给定条件:
必须:
- 以大写字母开头
- 以数字结尾
- 包含“cse”一词。
我被要求在一些代码作业中创建一个执行特定任务的方法,该方法应该检查所有必需的条件,方法的名称应该是 countTest 并且 它接受字符串作为参数.
我将向您展示我的代码,但我不知道如何创建此特定方法。
输出格式
System.out.println("There as a total number of words " + count + " and
the ones that fulfill the condition are: " + condition);
问题是,我不知道如何创建方法或构造函数或任何调用其中所有 3 个方法的方法,然后将该特定方法连接到主方法! 我希望你们能理解我是新手,提前谢谢你们!
public class D6_6 {
public static void main(String[]args){
Scanner sc = new Scanner(System.in);
System.out.println("Type a number that is at least 7");
int number = sc.nextInt();
int count = 0;
int condition = 0;
do{
if(number<7){
System.out.println("You should type a number that is at least 7 or higher");
number = sc.nextInt();
}
}
while(number<7);
sc.nextLine();
String str;
for(int i =0; i<number; i++){
System.out.println("Type a word");
str = sc.nextLine();
count++;
}
}
public boolean countTest(String str) {
}```
【问题讨论】:
-
charAt(int index)可用于获取指定索引处的字符。 一个简单的 for 循环可以帮助您实现您想要的。您面临什么问题? -
@AKSingh,我不知道如何创建此方法或放入其中的内容,以便它可以检查所有 3 个条件。就是这个问题,谢谢你的回复。
-
@user15793316 嗨,很抱歉。您可以在此处查看代码:pastebin.com/7bdtJN4H。非常感谢您的回复!
-
请发布代码而不是图片。你自己试过什么?
-
@user15793316 我真的明白,但由于某种原因,当我尝试将我的代码放在文本部分时,只有一半显示,另一半不起作用。
标签: java string methods contains startswith