From 47335045b4ba4401f9e836ffdd049c5dfc44ad2f Mon Sep 17 00:00:00 2001 From: xuxin <840198532@qq.com> Date: Wed, 22 Jul 2026 13:53:37 +0800 Subject: [PATCH] =?UTF-8?q?day08-=E5=B8=B8=E7=94=A8=E7=B1=BB-String-?= =?UTF-8?q?=E7=9A=84=E8=8E=B7=E5=8F=96=E6=96=B9=E6=B3=95=E7=BB=83=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day08/src/com/inmind/string01/Test05.java | 45 ++++++++++++++++++++++ day08/src/com/inmind/string01/Test06.java | 46 +++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 day08/src/com/inmind/string01/Test05.java create mode 100644 day08/src/com/inmind/string01/Test06.java diff --git a/day08/src/com/inmind/string01/Test05.java b/day08/src/com/inmind/string01/Test05.java new file mode 100644 index 0000000..e81befc --- /dev/null +++ b/day08/src/com/inmind/string01/Test05.java @@ -0,0 +1,45 @@ +package com.inmind.string01; + +import java.util.Scanner; + +/* +1.键盘录入QQ号码,验证格式的正确性。【charAt()看下每个字符】 + 必须是5—12位数字。 + 0不能开头。 + */ +public class Test05 { + public static void main(String[] args) { + //1.键盘输入QQ号码 + Scanner sc = new Scanner(System.in); + System.out.println("请输入QQ号码:"); + String qq = sc.nextLine(); + boolean result = checkQQ(qq); + System.out.println("当前QQ号码是否合法:"+result); + } + + public static boolean checkQQ(String qq) { + //必须是5—12位数字,长度和数字字符的验证 + //长度验证 + if (!(qq.length() >= 5 && qq.length() <= 12)) { + System.out.println("QQ号码长度必须在5-12位之间"); + return false; + } + //数字字符的验证 + for (int i = 0; i < qq.length(); i++) { + char c = qq.charAt(i); +// if (!(c >= '0' && c <= '9')) { + if (c < '0' || c > '9') { + System.out.println("QQ号码必须是数字"); + return false; + } + } + + //0不能开头 + if (qq.charAt(0) == '0') { + System.out.println("QQ号码不能以0开头"); + return false; + } + + return true;//符合QQ格式 + } +} diff --git a/day08/src/com/inmind/string01/Test06.java b/day08/src/com/inmind/string01/Test06.java new file mode 100644 index 0000000..9e1106c --- /dev/null +++ b/day08/src/com/inmind/string01/Test06.java @@ -0,0 +1,46 @@ +package com.inmind.string01; + +import java.util.Scanner; + +/* + +2.字符串查找。 【scanner indexOf 不停地找,直到找不到为止(死循环) index+1找】 + 键盘录入一个大字符串,再录入一个小字符串。 + 统计小字符串在大字符串中出现的次数。 + */ +public class Test06 { + public static void main(String[] args) { + //键盘录入一个大字符串,再录入一个小字符串。 + Scanner sc = new Scanner(System.in); + System.out.println("请输入一个长字符串:"); + String longStr = sc.nextLine(); + System.out.println("请输入一个短字符串:"); + String shortStr = sc.nextLine(); + int count = getCount(longStr, shortStr); + System.out.println("小字符串在大字符串中出现的次数为:"+count); + } + + //统计小字符串在大字符串中出现的次数。 + public static int getCount(String longStr, String shortStr) { + int count = 0;//统计出现的次数 + + //public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引,如果找不到是-1 + //分析:也就是indexOf方法,查找shortStr,返回-1就是找不到了,就结束查询了 + int index = 0;//记录每次查找子字符串的索引,便于下一次查找(循环条件的参数) + /*while (index != -1) { + index = longStr.indexOf(shortStr,index); + if (index != -1) { + //下一次查找就得从index+1开始 + index++; + count++; + } + }*/ + + while ((index = longStr.indexOf(shortStr,index)) != -1) {//将index的赋值与判断放在一起同时作为循环条件 + //下一次查找就得从index+1开始 + index++; + count++; + } + return count; + } +}