【问题标题】:Method to check if the string contains certain elements that accepts a parameter String检查字符串是否包含接受参数 String 的某些元素的方法
【发布时间】:2026-01-06 01:00:02
【问题描述】:

我是一名学生,对 Java 还很陌生。对于我的家庭作业,我必须:

  • 要求用户使用 do while 循环输入一个数字(至少 7 个)。

  • 使用 for 循环,我需要要求用户输入 that 字数。

  • 然后我必须检查其中一个词是否满足给定条件:

必须:

  1. 以大写字母开头
  2. 以数字结尾
  3. 包含“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


【解决方案1】:
  1. 检查单词是否以大写开头: 您可以通过首先通过str.charAt(0) 选择要检查的字符来做到这一点。这将返回一个字符,它是输入 str 的第一个字母。
    要检查此字符是否为大写字母,您可以轻松使用char.isUppercase()。这将返回一个布尔值。如果您将str.charAt(0) 的字符放入其中,则必须将char 替换为变量的名称。

  2. 检查最后一个字符是否为数字: 您可以通过首先选择str.charAt(str.length()-1) 的最后一个字符来再次执行此操作,string.length-1 是最后一个字符的编号。
    要检查此字符是否为数字,可以使用 ascii 表。每个角色都有自己的编号。所以如果你想检查你的字符是否在0到9之间,你可以使用char &gt;= 48 || char &lt;= 57(在ascii表中查找)。同样,char 是变量的名称,您将 str.charAt(str.length()-1) 的字符放入其中。

  3. 检查单词是否包含“cse”: 有一个非常简单的方法:str.contains("cse") 将返回一个布尔值,当单词中包含“cse”时为真,当单词不包含“cse”时为假。

我希望你现在清楚了!

【讨论】:

  • 嗨@Sven,非常感谢您的回复和回答。当我执行所有这些操作时,我对如何访问在 main 方法中创建了所有这些条件的方法感到困惑,这就是我的问题。我的问题是方法,它应该是布尔值还是字符串还是 idk?那么我如何将它连接到 main 方法,确切地说是在 for 循环中。再次感谢您!
  • @Eno 我不确定你希望你的输出是什么。您是否想要类似:“单词总数为 7,满足条件的单词为:Scent9 Sce45”?
  • 输出应该是这样的:总字数为7,满足条件的有:+condition(满足条件的。
【解决方案2】:

我想我做到了,非常感谢你们,我很感激!

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++;
            if((countTest(str))){
                condition++;

            }
        }
        if(count == 0){
            System.out.println("No words typed");
        } else {
            System.out.println("Total number of words typed: " + count + ", which fulfill the condition: "+ condition);
        }
    }

    public static boolean countTest(String str) {
        return Character.isUpperCase(str.charAt(0)) && str.charAt(str.length() - 1) >= 48 || str.charAt(str.length() - 1) <= 57 || str.contains("cse");
    }

}```

【讨论】:

    最近更新 更多