Java 方法检查字符串是否包含接受参数字符串的某些元素
我是一名学生,对Java有点陌生。对于我的家庭作业,我必须:Java 方法检查字符串是否包含接受参数字符串的某些元素,java,string,methods,contains,startswith,Java,String,Methods,Contains,Startswith,我是一名学生,对Java有点陌生。对于我的家庭作业,我必须: 要求用户使用do-while循环输入一个数字(至少7) 使用for循环,我需要要求用户输入,即字数 然后我必须检查其中一个词是否满足给定的条件: 这个词必须: 以大写字母开头 以数字结尾 包含“cse”一词 我被要求在一些代码作业中创建一个方法,该方法执行特定任务,该方法应检查所有必需的条件,该方法的名称应为countTest,并接受字符串作为参数 我将向您展示我的代码,但我不知道如何创建这个特定的方法 输出格式 Syst
- 要求用户使用do-while循环输入一个数字(至少7)
- 使用for循环,我需要要求用户输入
,即
字数
- 然后我必须检查其中一个词是否满足给定的条件:
countTest
,并接受字符串作为参数
我将向您展示我的代码,但我不知道如何创建这个特定的方法
输出格式
System.out.println("There as a total number of words " + count + " and
the ones that fulfill the condition are: " + condition);
问题是,我不知道如何创建方法或构造函数,或者调用调用其中所有3个方法的任何方法,然后将特定方法连接到主方法! 我希望你们能理解我是新手,提前谢谢你们
公共级D6_6{
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
System.out.println(“键入一个至少为7的数字”);
int number=sc.nextInt();
整数计数=0;
int条件=0;
做{
如果(数字)
要检查单词是否以大写字母开头,请执行以下操作:
您可以首先选择要通过str.charAt(0)
检查的字符。这将返回作为输入str
的第一个字母的字符
要检查此字符是否为大写字母,可以轻松使用char.isUppercase()
。这将返回一个布尔值。如果将str.charAt(0)
的字符放入,则必须将char
替换为变量名称
要检查最后一个字符是否为数字,请执行以下操作:
您可以再次执行此操作,首先通过str.charAt(str.length()-1)
,WAREstring选择最后一个字符。length-1
是最后一个字符的编号。
要检查这个字符是否是数字,可以使用ascii表。每个字符都有自己的数字。因此,如果要检查字符是否介于0和9之间,可以使用char>=48 | | char我想我做到了,非常感谢大家,我非常感谢
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");
}
}```
公共级D6_6{
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
System.out.println(“键入一个至少为7的数字”);
int number=sc.nextInt();
整数计数=0;
int条件=0;
做{
如果(数字<7){
System.out.println(“您应该键入一个至少为7或更高的数字”);
编号=sc.nextInt();
}
}
而(数量<7);
sc.nextLine();
字符串str;
for(int i=0;i 返回字符.isUpperCase(str.charAt(0))&&str.charAt(str.length()-1)>=48 | | str.charAt(str.length()-1)charAt(int索引)
可用于获取指定索引处的字符。简单的for循环可以帮助您实现所需。您面临的问题是什么?@AKSingh,我不知道如何创建此方法,也不知道如何放入此方法以检查所有3个条件。这就是问题所在,谢谢您的回复。@user15793316 Hi,很抱歉。您可以检查这里的代码:。非常感谢您的回复!请发布代码而不是图片。您自己尝试过什么?@user15793316我确实理解,但由于某种原因,当我尝试将代码放入文本部分时,只有一半显示,另一半不起作用。嗨@Sven,非常感谢您的回复和回答。当我完成所有这些时,我会我对如何访问我在主方法中创建的所有这些条件的方法感到困惑,这是我的问题。我的问题是方法,它应该是布尔值还是字符串或idk?那么我如何将其连接到主方法,确切地说是在for循环中。再次感谢!@Eno我不确定您希望输出的是什么。您想要吗比如:“总字数是7,满足条件的是:Scent9 Sce45”?输出应该是这样的:总字数是7,满足条件的是:+condition(满足条件的)。
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");
}
}```