计算字符串中子字符串出现的次数
1.计算字符串中子字符串出现的次数:让用户分别输入字符串和子字符串,输出子字符串出现的次数,程序运行结果如图5.7所示。package gaoshuang;import java.util.Scanner;public class StrCoun {public static void main(String[] args) {int count = 0; //用于计数的变量int start =
·
1.计算字符串中子字符串出现的次数:让用户分别输入字符串和子字符串,输出子字符
串出现的次数,程序运行结果如图5.7所示。
package gaoshuang;
import java.util.Scanner;
public class StrCoun {
public static void main(String[] args) {
int count = 0; //用于计数的变量
int start = 0; //标识从哪个位置开始查找
Scanner input = new Scanner(System.in);
System.out.print("请输入一个字符串:");
String str = input.next();
System.out.print("请输入要查找的字符串:");
String str1 = input.next(); while (str.indexOf(str1, start) >= 0 && start < str.length()) {
count++;
start = str.indexOf(str1, start) + str1.length(); //找到子字符串后,查找位置移动到找到的这个字符串之后开始
}
System.out.println(str1 + " 在 " + str + "出现的次数为" + count);}

package gaoshuang;
import java.util.Scanner;
public class EngRegister{
//使用 verify 方法对用户名、密码进行验证,返回是否成功
public static boolean verify(String name,String pwd1,String pwd2){
boolean flag = false;//标识是否成功
if(name.length() < 6 || pwd1.length() < 8){
System.out.println("用户名长度不能小于 6,密码长度不能小于 8!");
}else if((pwd1.equals(pwd2))==false){
System.out.println("两次输入的密码不相同!");
}else{
System.out.println("注册成功!请牢记用户名和密码。");
flag = true;
}
return flag;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String engName,p1,p2;
boolean resp = false;//标识是否成功
do{
System.out.print("请输入 Java 工程师用户名: "); engName = input.next();
System.out.print("请输入密码: ");
p1 = input.next();
System.out.print("请再次输入密码: ");
p2 = input.next();
verify(engName,p1,p2);//【代码 3】//调用 verify 方法对用户名、密码进行验证,返回是否成功
}while(!resp);
}
}

package gaoshuang;
import java.util.Scanner;
public class FileUpload{
public static void main(String[] args) {
boolean fileCorrect = false; //标识论文文件名是否正确
boolean emailCorrect = false; //标识邮箱是否正确
System.out.println("请按照下面要求提交论文");
Scanner input = new Scanner(System.in);
System.out.print("请输入论文文件名(必须以.docx 结尾):");
String fileName = input.next();
System.out.print("请输入接收论文反馈的邮箱:");
String email = input.next();
//检查论文文件名
if(fileName.indexOf(".docx")>-1){
fileCorrect = true; //标识论文文件名正确
}else{
System.out.println("文件名无效!");
}
//检查邮箱格式
if(fileCorrect&&emailCorrect){
emailCorrect = true; //标识邮箱正确
}else{
System.out.println("邮箱无效!");
}
//输出结果
if(fileCorrect&&emailCorrect){
System.out.println("论文提交成功!");
}else{
System.out.println("论文提交失败!");
}
}
}

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐


所有评论(0)