java学习第八天内容:
一、String类的认识及常用方法
二、static关键字
三、Array类
四、Math类
一、String类
String表示字符串
String的特点:
1)Java程序中的所有字符串字面值(例如"abc" 、“你好”、"“空字符串、” "空格字符串)都被实现为此类的实例(即对象)。(直接写上“”就是创建一个空字符串)
程序中直接写上双引号字符串,就以在字符串常量池中
2)字符串是常量,它们的值在创建之后不能更改。
1.String的构造方法:构造方法用来创建对象
根据已有的byte[]和char[]都能创建字符串对象
API提供的字符串的构造方法有:
//1.创建一个空的字符串对象 “”
public String()
//2.根据一个字节数组来创建字符串对象
public String(byte[] bytes)
//3.根据一个字节数组的一部分来创建字符串对象。
public String(byte[] bytes, int offset, int length)
//4.根据一个字符数组来创建字符串对象
public String(char[] value)
//5.根据一个字符数组的一部分来创建字符串对象。
public String(char[] value, int offset, int count)
举例:
//1.利用空参构造方法创建对象,就是一个""
String str1 = new String();//调用string类的空参构造器
System.out.println(str1);//空字符串
//2.利用byte[]创建字符串对象,会根据字节和编码的对应关系把字节转换成字符串
byte[] bs={97,98,99,100};
String str2 = new String(bs);
System.out.println(str2);
//3.利用byte[]的一部分创建字符串对象,从0开始把2个字节转换为字符串
String str3=new String(bs,0,2);//指定字节数组元素的索引
System.out.println(str3);
//4.利用char[]创建字符串对象,把每个字符拼接成一个字符串
char[] chs = {'a','b','c','d','e','f'};
String str4 = new String(chs);
System.out.println(str4);
//5.利用char[]字符数组的一部分元素创建字符串对象,从索引1开始把2个字符拼接为字符串
String str5 = new String(chs,1,2);//字符数组对象,起始索引 ,字符元素个数
System.out.println(str5);
运行结果:
//空字符串
abcd
ab
abcdef
bc
3.String类提供的常用方法
1)判断功能:
方法调用格式:String对象.方法名
//1.判断字符串是否相等
public boolean equals(Objects obj)//参数:Object可以接受任何类型的数据,这里接受字符串
两种字符串对象的区别
常量池是jvm在堆内存中单独划分出来的一片空间,字符串字面值存储在常量池中。
String s1="abc"; /在常量池中创建并存储在常量池中,s1保存的是常量池的地址
String s2="abc"; //s2和s1存储的是同一个地址("abc"在常量池中已经存储,不会重复创建,复用原来的地址即可)
String s3=new String("abc"); //new出来的字符串直接在堆内存中,,s2保存的是堆的地址
String s4="a"+"bc"; //编译后 s4="abc";
System.out.println(s1==s2); //true
System.out.println(s1==s3); //fasle
System.out.println(s1==s4); //true
两种字符串对象的内存分析图解:
equals方法的推荐写法:
//2.判断字符串是否相等,并忽略字母大小写
public boolean equalsIgnoreCase(Objects obj);//返回一个boolean类型的结果
注意:只可以忽略英文字母大小写:
//3.判断字符串以什么后缀结尾(或判断字符串的后缀)
public boolean endsWith(String str);//返回一个布尔值
//4.判断字符串以什么前缀开始(判断字符串的开头)
public boolean startsWith(String str);//返回一个布尔值
//5.判断字符串是否包含某一子串
public boolean contains(String str);//返回一个布尔值
代码测试:
public class Demo2 {
public static void main(String[] args) {
String str="今天中午吃5碗热干面,在加两个鸡蛋";
boolean b1 = str.startsWith("今天");
System.out.println(b1);
boolean b2 = str.endsWith("鸡蛋");
System.out.println(b2);
boolean b3 = str.contains("热干面");
System.out.println(b3);
}
}
运行结果:
true
true
true
2)获取功能
方法调用格式:String对象.方法名
//1.获取字符串的长度
public int length();//返回int类型的结果
//2.查找字符串指定索引位置的字符
public char charAt(int index);//返回一个字符
//3.在已有字符串后面拼接字符串
public String concat(String str);
//4.根据字符获取第一次出现的索引
public int indexOf(int ch);//参数:可以接受字符(‘a’)、接受字符的编码(97)
返回值:如果找不到,返回-1,如果找到返回字符对应的索引
//5.查找某一子字符串在字符串中第一次出现的索引位置(返回以该子字符串的第一个字符的索引)
public int indexOf(String str);//把字符串作为参数传入,返回索引值
String str="HelloJavaWorldJava";
int index = str.indexOf("Java");
System.out.println(index);
运行结果:
5
3).截取功能
方法调用格式:String对象.方法名
//1.从指定的索引开始和指定的索引为结束截取字符串的一部分
public String subString(int beginIndex,int endIndex);//返回字符串
//注意:截取的字符串包含开始索引的字符,但不包含结束索引对应的字符
//2.从指定的索引开始一直截取到字符串的末尾
public String subString(int beginIndex);//返回字符串
代码测试:
String str="床前明月光,疑是地上霜";
//从6索引开始,截取到末尾
String sub1 = str.substring(6);
System.out.println(sub1);
//从0索引开始,到5索引结束(不包含5索引)
String sub2 = str.substring(0, 5);
System.out.println(sub2);
运行结果:
疑是地上霜
床前明月光
4)转换功能
方法调用格式:String对象.方法名
//1.把字符串中的小写字母转换为大写字母
public String toUpperCase();//传入字符串
//2.把字符串中的大写字母转换为小写字母
public String toLowerCase();//传入字符串,返回字符串
//3.把字符串转换为字符数组 char[]
public char[] toCharArray();
char[] chars = str.toCharArray();//str 是字符串对象,通过该对象调用String类的toCharArray()方法,实现将该对象所管理的字符串转换为字符并存储到字符数组
//4.把字符串转换为字节数组 byte[]
public byte[] getBytes();
代码测试:
String str="HelloWorld";
//把字符串转换为大写
String s1 = str.toUpperCase();
System.out.println(s1);
//把字符串转换为小写
String s2 = str.toLowerCase();
System.out.println(s2);
//把字符串转换为字符数组
char[] chs = str.toCharArray();
for (int i = 0; i < chs.length; i++) {
System.out.println(chs[i]);
}
System.out.println("-------------");
//把字符串转换为字节数组
byte[] bs = str.getBytes();
for (int i = 0; i < bs.length; i++) {
System.out.println(bs[i]);
}
运行结果:
HELLOWORLD
helloworld
//把字符串转换为字符数组
H
e
l
l
o
W
o
r
l
d
-------------
//把字符串转换为字节数组(根据ASCII编码的对应关系来转换)
72
101
108
108
111
87
111
114
108
100
5).切割功能
//1.把一个字符串按照指定的字符串切割点切割成多个字符串并存储到字符串数组中
public String[] split(String str);//传入指定的切割点(字符串)
//注意:.表示任意字符串 //.才表示从字符串中的.位置处切割。切割后的字符串中不包含指定的切割点(因为被切没了)
//也可以指定从字符串的空格处作为切割点切割,因为空格也是字符串的一种
代码测试:
String str="www.itcast.com";
String[] array = str.split("\\.");//不能以.为切割点,否则会把字符串切割没的,因为.表示把任意字符作为切割点
// 切割成字符串存储到字符数组中
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
运行结果:
www
itcast
com
6)其他功能
方法调用格式:String对象.方法名
//1.去掉字符串前后的空格
public String trim();
String str2=" helloworld ";
System.out.println(str2.trim());
运行结果:
helloworld
//2.按照字符在字典中的排列顺序比较两个字符串第一个字符的大(在字典中排在后面) 小 (在字典中排在前面)
public int compareTo(String anotherString);
返回值:
正数:该字符串比传入的参数字符串大
负数:就是小
0:大小相等
代码测试:
public class Demo03 {
public static void main(String[] args) {
String str1="a";
int int1 = str1.compareTo("a");
System.out.println(int1);
System.out.println("------");
String str2 = "bacc";
int int2 = str2.compareTo("c");
int int22 = str2.compareTo("bccc");
int int222 = str2.compareTo("bccce");
System.out.println(int2);
System.out.println(int22);
System.out.println(int222);
System.out.println("------");
String str3 = "b";
int int3 = str3.compareTo("a");
System.out.println(int3);
}
}
运行结果:
0
------
-1
-2
-2
------
1
//3.字符串的拼接(实际上是创建了一个新的字符串)
System.out.printl(“str1”.concat(“str2”));
System.out.printl("abc".concat("def"));<==>System.out.printl("abc"+"def");常量做运算时,在编译的时候已经运算好了
//4.将字符数组类型数据转换为字符串形式并打印
public static String valueOf(char[],ch)
阶段练习:键盘录入 一个字符串,计算其中每中类型的字符出现的次数
步骤分析:
1.使用Scanner,实现键盘录入一个字符串
2.计算每种类型的字符串出现的次数
a:定义4个计数器(分别记录大写字母、小写字母、数字字符、其他字符出现的次数)
- count1 计数大写字母
- count2 计数小写字母
- count3 计数数字字符
- count4 计数其他字符
b:遍历字符串中的每一个字符 ch
c:对每个字符进行判断
- 如果ch>=‘A’ && ch<=‘Z’ count1++
- 如果ch>=‘a’ && ch<=‘z’ count2++
- 如果ch>=‘0’ && ch<=‘9’ count3++
- 否则 以上都不成立 count4++
代码部分:
//1.创建scanner对象,并通过对象调用该类的next()方法,实现录入一个字符串
Scanner sc = new Scanner(System.in);
System.out.println("录入字符串:");
String str = sc.next();//录入字符串,并交给String类型的str变量管理
//2.定义计数器并初始化
int count1 = 0;
int count2 = 0;
int count3 = 0;
int count4 = 0;
//3.遍历字符串中的每个字符ch
for(int i=0;i<str.length();i++){
//获取字符串中i索引位置的字符,并交给char类型的ch变量管理
char ch = str.charAt(i);//赋值给变量char,根据utf-8/us-ascii/iso-8859-1编码规定的字符char和int整数之间的对应关系实际上变量char存储的是int类型的数值
// 对字符进行判断
if(ch>='A' && ch<='Z'){
count1++;
}else if(ch>='a' && ch<='z'){
count2++;
}else if(ch>='0' && ch<='9'){
count3++;
}else{
count4++;
}
}
//4.打印4个计数器
System.out.println("大写字母:"+count1);
System.out.println("小写字母:"+count2);
System.out.println("数字字符:"+count3);
System.out.println("其他字符:"+count4);
把数组转换为指定格式字符串输出:
package com.itheima.Test;
public class Demo03 {
public static void main(String[] args) {
int[] array={1,2,3,4};
String string = arrayToString(array);
System.out.println(string);
}
//把数组转换为字符串
public static String arrayToString(int[] array){
//1.先定义一个字符串 "{"
String restult="{";
//2.遍历数组
for (int i = 0; i < array.length; i++) {
int e = array[i];
//如果是最后一个元素 拼接:元素+"}"
if(i==array.length-1){
restult+=array[i]+"}";
}else{
//如果是最后一个元素 拼接:元素+"#"
restult+=array[i]+"#";
}
}
return restult;
}
}
运行结果:
{1#2#3#4}
二、static关键字
static(静态的)只可以修饰方法和变量(final关键字可以用来修饰类,修饰变量,修饰方法),有如下特点:
1).static关键字修饰成员的属于类、随着类的加载而加载
2).静态能被该类所有对象共享
3).static修饰的方法,既能被该类的对象调用,也可以通过类名来调用,推荐使用l类名.静态方法();来调用
静态关键字修饰的变量:
static修饰的变量是属于类的,也叫做类变量,可以被该类的所有的对象共用,类加载,被创建的时候静态变量也就一起加载,被创建。用类名调用即可(不建议使用对象名调用)
静态修饰的方法:
1).静态方法中不能有this关键字
2).静态方法只能调用静态方法
不能调用非静态的方法
但非静态方法可以调用静态方法
静态方法的内存图解:
静态的使用:
工具类:把一个类中的方法,都写成静态的,直接通过类名调用即可(即类名.方法名()),这样简化了代码,不用每次通过构造对象,让对象来调用方法,这样比较麻烦
三、Arrays工具类
Arrays是用于操作数组的工具类
//1.对数组进行排序
public static void sort(int[] array);//传入int类型的数组
//2.把一个数组元素转换为字符串 格式:[元素1,元素2,…]
public static String toString(int[] array);
//3.使用二分查找法,查找关键字key在array中出现的索引
条件:1.数组必须是排序好了的 2)元素不能有重复
四、Math类
Math类是用于做数学运算的工具类(该类提供的方法都是静态方法,可以直接通过类名调用方法,不用创建对象,通过对象来调用方法)
System.out.println(Math.max(a,b)); //如果a=3,b=4 打印输出4
System.out.println(Math.min(a,b)); //打印输出3
//2.求一个数的绝对值
正数的绝对值就是本身
System.out.println(Math.abs(-a));//输出a
System.out.println(Math.abs(a));//输出a
System.out.println(Math.ceil(3.1));//输出4.0
System.out.println(Math.floor(3.9));//输出3.0
System.out.println(Math.round(3.5)); //4
System.out.println(Math.round(3.4)); //3
System.out.println(Math.pow(a,b)); //a的b次方:a^b
System.out.println(Math.pow(b,a)); //b的a次方:b^a
System.out.println(Math.sqrt(4));//2
System.out.println(Math.sqrt(5));//2.23606797749979
数据类型分类:
1.基本数据类型(4类8种)
2.引用类型:string、类、数组
char chars[] = {'a','b','c'};//C语言中的数组创建方式-Java也兼容
char[] char= {'a','b','c'};//Java中静态创建一个字符数组
关于final关键字:
- 是一个关键字,表示最终的意思。可以用来修饰类,修饰变量,修饰方法。
- 修饰类:表明该类是最终类,不能被继承
- 修饰变量:表明该变量是常量,不能再次被赋值
- 修饰方法:表明该方法是最终方法,不能被重写
int i = 1, j = ++i+ (++i) + i--; 后,i与j的值分别为2,8
代码块的结构:
{
方法体
}
直接打印字符数组-结果不是地址值是内容-因为底层的实现方法不同
public class Demo03 {
public static void main(String[] args) {
char[] chars = {'1','a','#'};
System.out.println(chars);
}
}
运行结果:
1a#
对比字符串数组:
public class Demo03 {
public static void main(String[] args) {
String[] str = {"shf","ufh"};
System.out.println(str);
}
运行结果:
[Ljava.lang.String;@50cbc42f//数组在堆内存中创建并存储的空间的地址
char的数据范围比int类型的数据范围小
int a = ‘a’;//97
字符’0’和0是不一样的
‘0’:在ASCII编码中对应的是:48
代码练习:
1.
package com.itheima.Test;
public class Test01 {
public static void main(String[] args) {
byte[] bs={-23, -108, -82, -25, -101, -104, -26, -107, -78, -25, -125, -126, -17, -68, -116, -26, -100, -120, -24, -106, -86, -24, -65, -121, -28, -72, -121};
String str = new String(bs);//传入创建字符数组对象bs参数,会根据字节和编码的对应关系字节转换为字符串
//打印字符串内容
System.out.println(str);
//替换目标字符串
String newstr= str.replace("键盘","手指");
newstr = newstr.replace("万","千万");
System.out.println(newstr);
//用,号切割,得到一个字符串数组,并遍历字符串数组中的元素,打印输出
String[] str2 = newstr.split(",");
for (int i = 0; i < str2.length; i++) {
System.out.println(str2[i]);
}
}
}
运行结果:
键盘敲烂,月薪过万
手指敲烂,月薪过千万
手指敲烂
月薪过千万
package com.itheima.Test;
public class Test02 {
public static void main(String[] args) {
String[] str1 = {"hello","world"," java ","android"};
String string = arrayToSting(str1);
System.out.println(string);
//替换目标字符串
String str2 = string.replace("java", "爪哇");
System.out.println(str2);
//截取指定索引区间的子字符串
String substr = str2.substring(10, 16);
System.out.println(substr);
//去掉上一步字符串中的空格
String[] split = substr.split(" ");//以空格为切割点,切割成两个字符串并存放在字符串数组中
String str3 = arrayToSting(split);//把字符数组的元素拼接成字符串
String trim1 = str3.trim();//trim只能去除字符串前后的空格,不能去除字符串中间的空格
System.out.println(trim1);
System.out.println("------------------");
//把上一步字符串中的字符转换为大\小写并输出
String str4 = trim1.toUpperCase();
System.out.println(str4);
String str5 = str4.toLowerCase();
System.out.println(str5);
}
public static String arrayToSting(String[] str) {
String result = "";
for (int i = 0; i < str.length; i++) {
result += str[i];
}
return result;
}
}
运行结果:
helloworld java android
helloworld 爪哇 android
爪哇 an
爪哇an
------------------
爪哇AN
爪哇an
package com.itheima.Test;
import java.util.Arrays;
public class Test03 {
public static void main(String[] args) {
int[] array = {5,4,3,2,1};
Arrays.sort(array);//数组排序
String str = Arrays.toString(array);//按照默认的格式打印
System.out.println(str);
//二分查找
int i = Arrays.binarySearch(array,2);
System.out.println(i);
}
}
运行结果:
[1, 2, 3, 4, 5]
1
package com.itheima.Test;
public class Test04 {
public static void main(String[] args) {
//Math类的绝对值
System.out.println(Math.abs(-100));
System.out.println(Math.abs(200));
//求最大值
System.out.println(Math.max(3,50));
//求最小值
System.out.println(Math.min(3,50));
//求次幂(2的3次幂)
System.out.println(Math.pow(2,3));
//求平方根(4开平方)
System.out.println(Math.sqrt(4));//2.0
//PI的使用
System.out.println(Math.ceil(Math.PI*Math.pow(3,2)));
System.out.println(Math.floor(2*Math.PI*3));
}
}
运行结果:
100
200
50
3
8.0
2.0
29.0
18.0
5.判断字符串中子字符串出现的次数
方法一:数学思维
package com.itheima.Test;
//数学思维
public class Test06_02 {
public static void main(String[] args) {
//1.创建一个字符串
String str = "heimaabcheimabbbheimacccheimadddheimacccheima";
String replaceStr = str.replaceAll("heima", "");
int count = (str.length()-replaceStr.length())/"heima".length();
System.out.println("heima个数:"+count+"个");
}
}
运行结果:
heima个数:6个
方法二:substring方法截取字符串
package com.itheima.Test;
public class Test06_03 {
public static void main(String[] args) {
//1.创建一个字符串
String str1 = "heimaabcheimabbbheimacccheimadddheimacccheima";
int index ;
int count=0;
while ((index = str1.indexOf("heima")) != -1) {//通过indexOf()方法查找目标字符串第一次出现的索引位置,如果不存在就返回-1
str1 = str1.substring(index +"heima".length());//通过subString()方法指定截取字符串的起始索引处("heima"后面开始)截取剩下的全部字符串
count++;
}
System.out.println("heima字符串的个数:"+count+"个");
}
}
运行结果:
heima字符串的个数:6个
方法三:使用KMP算法,统计次数
package com.itheima.Test;
/**
*判断字符串中子字符串出现的次数
* 使用KMP算法,统计次数
*/
public class Test06_01 {
public static void main(String[] args) {
//1.创建一个字符串
String str1 = "heimaabcheimabbbheimacccheimadddheimacccheima";
String str2 = "heima";
int int1 = str1.indexOf("heima");
System.out.println(int1);
int time = timesAll(str1, str2);
System.out.println("heima个数:"+time+"个");
}
public static int timesAll(String str1, String str2) {
//计数器
int time = 0;
try {
char[] s1 = str1.toCharArray(); // byte s1 [] = s.getBytes();
// 分别转化成char,byte类型的数组,其余
char[] t1 = str2.toCharArray(); // byte t1 [] = t.getBytes();
// 操作都一样
int i = 0, j = 0;
while (i < s1.length && j < t1.length) {
if (s1[i] == t1[j]) {
i++;
j++;
if (j == t1.length) {
j = 0;
time++;
}
} else {
i = i - j + 1;
j = 0;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return time;
}
}
运行结果:
0
heima个数:6个
- 随机生成一个长度为10的字符串(字符串中可以大小写字母和数字)
小提示:
1)先想办法把’a’‘z’,‘A’‘Z’,‘0’~'9’所有的字符存储在ArrayList集合中
2)从集合中随机的获取一个字符,获取10次,拼接成一个字符串并返回
方法一:
package com.itheima.Test;
import java.util.ArrayList;
import java.util.Random;
/*
随机生成一个长度为10的字符串(字符串中可以大小写字母和数字)
小提示:
1)先想办法把'a'~'z','A'~'Z','0'~'9'所有的字符存储在ArrayList集合中
2)从集合中随机的获取一个字符,获取10次,拼接成一个字符串并返回
*/
public class Test07_02 {
public static void main(String[] args) {
String randomStr = getRandomString(10);
System.out.println(randomStr);
}
public static String getRandomString(int n) {
ArrayList<Character> list=new ArrayList<>();
//验证码的生成原理如此
//把'a'~'z'存储到ArrayList集合中
for(char ch='a';ch<='z';ch++){
list.add(ch);
}
//把'A'~'Z'存储到ArrayList集合中
for(char ch='A';ch<='Z';ch++){
list.add(ch);
}
//把'0'~'9'存储到ArrayList集合中
for(char ch='0';ch<='9';ch++){
list.add(ch);
}
//随机获取长度为n的字符串
Random r=new Random();
String str="";
for(int i=0;i<n;i++){
int index = r.nextInt(list.size());
Character ch = list.get(index);
str+=ch;
}
//把字符串返回
return str;
}
}
运行结果:
xiuOjloK0B
方法二:
package com.itheima.Test;
import java.util.ArrayList;
import java.util.Random;
// 随机生成一个长度为10的字符串(字符串中可以大小写字母和数字)
// 小提示:
// 1)先想办法把'a'~'z','A'~'Z','0'~'9'所有的字符存储在ArrayList集合中
// 2)从集合中随机的获取一个字符,获取10次,拼接成一个字符串并返回
public class Test07_01 {
public static void main(String[] args) {
int length =10;
String str = RandomString(length);
System.out.println("随机字符串:"+str);
System.out.println("--------------------");
char[] chars = str.toCharArray();
System.out.println("chars:"+chars);
ArrayList<Character> list = new ArrayList<>();
for (int i = 0; i < chars.length; i++) {
list.add(chars[i]);
}
System.out.println("list:"+list);
Random random = new Random();
ArrayList<Character> list1 = new ArrayList<>();
for (int i = 0; i < 10; i++) {
int int1 = random.nextInt(10);
list.get(int1);
list1.add(list.get(int1));
}
System.out.println("list1-->:"+list1);
String str1="";
for (int i = 0; i < list1.size(); i++) {
str1+=list1.get(i);
}
System.out.println("从集合中随机的获取字符串:"+str1);
}
//随机生成长度为10的字符串
public static String RandomString(int length) {
char[] ch = new char[length];
int i=0;
while(i<length) {
int f = (int) (Math.random()*3);//?
if(f==0) {
ch[i] = (char) ('A' + Math.random() * 26);
} else if(f==1) {
ch[i] = (char) ('a' + Math.random() * 26);
}else {
ch[i] = (char) ('0' + Math.random() * 10);
}
i++;
}
String str=new String(ch);
return str;
}
}
运行结果:
随机字符串:nqHCUwdU72
--------------------
chars:[C@2133c8f8//地址
list:[n, q, H, C, U, w, d, U, 7, 2]
list1-->:[2, U, 7, n, n, C, U, H, d, q]
从集合中随机的获取字符串:2U7nnCUHdq
小练习:
编写一个Java程序,提示用户输入一个字符串,要求字符串必须存在字母(需要代码判断)
1.若不符合要求,则提示用户重新输入直至符合要求为止
2.若符合要求,则判断字符串中大写字母出现的次数
代码:
package com.itheima.Test03;
/*
编写一个Java程序,提示用户输入一个字符串,要求字符串必须存在字母(需要代码判断)
1.若不符合要求,则提示用户重新输入直至符合要求为止
2.若符合要求,则判断字符串中大写字母出现的次数
*/
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
//1.Scanner实现键盘录入字符串
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个字符串:");
//3.计数器
int count1 = 0;
int count2 = 0;
//2.获取字符串中的每个字符,并循环遍历,条件判断
//4.while循环实现重复输入
while (true){
String str = scanner.nextLine();
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if((ch>='A' && ch<='Z') || (ch>='a' && ch<='z')){
if (ch>='A' && ch<='Z'){
count1++;
}
if (ch>='a' && ch<='z'){
count2++;
}
}
}
//把计数器单独拿出来做判断,就可以避免for循环对break语句的干扰
if (count1 != 0 || count2 != 0){
System.out.println("字符串中大写字母的个数:"+count1+"个");
break;
}else {
System.out.println("请重新输入含有字母的字符串:");
}
}
}
}
测试结果:
请输入一个字符串:
232312
请重新输入含有字母的字符串:
1312大师傅但是
请重新输入含有字母的字符串:
大师傅撒
请重新输入含有字母的字符串:
3DSDss
字符串中大写字母的个数:3个
Java学习第九天内容:
https://blog.csdn.net/LJN951118/article/details/88920679