常用类章节练习
1、编程题
(1)将字符串中指定部分进行反转。比如将"abcdef"反转为"aedcbf"
(2)编写方法 public static String reverse(String str, int start, int end)搞定
package com13.exercise;
/**
* @author 甲柒
* @version 1.0
* @title Exercise01
* @package com13.exercise
* @time 2023/1/12 18:49
*/
public class Exercise01 {
public static void main(String[] args) {
String str = "abcdefgh";
System.out.println("=====交换前=====");
System.out.println(str);
try {
str = reverse(str, 1, 6);
} catch (Exception e) {
System.out.println(e.getMessage());
return;
}
System.out.println("=====交换后=====");
System.out.println(str);
}
public static String reverse(String str, int start, int end) {
//对输入的参数进行验证
//(1)写出正确的情况
//(2)然后取反
if (!(str != null && start >= 0 && end > start && end < str.length())) {
throw new RuntimeException("参数不正确");
}
char[] chars = str.toCharArray();
char temp = ' ';//交换辅助变量
for (int i = start, j = end; i < j; i++, j--) {
temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
}
//使用chars重新构建一个String返回即可
return new String(chars);
}
}
2、编程题
输入用户名、密码、邮箱,如果信息录入正确,则提示注册成功,否则生成异常对象要求:
(1)用户名长度为2或3或4
(2)密码的长度为6,要求全是数字
(3)邮箱中包含@和.并且@在.的前面isDigital
package com13.exercise;
/**
* @author 甲柒
* @version 1.0
* @title Exercise02
* @package com13.exercise
* @time 2023/1/12 19:07
*/
public class Exercise02 {
public static void main(String[] args) {
String name = "tom";
String pwd = "123456";
String email = "[email protected]";
try {
userRegister(name, pwd, email);
System.out.println("注册成功~~~");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public static void userRegister(String name, String pwd, String email) {
if (!(name != null && pwd != null && email != null)) {
throw new RuntimeException("参数不能为空");
}
int userLength = name.length();
if (!(userLength >= 2 && userLength <= 4)) {
throw new RuntimeException("用户名长度为2或3或4");
}
if (!(pwd.length() == 6 && isDigital(pwd))) {
throw new RuntimeException("密码长度是6,要求全是数字");
}
int i = email.indexOf('@');
int j = email.indexOf('.');
if (!(i > 0 && j > i)) {
throw new RuntimeException("邮箱中包含@和. 并且@在.前面");
}
}
public static boolean isDigital(String str) {
char[] chars = str.toCharArray();
for (int i = 0; i < chars.length; i++) {
if (chars[i] < '0' || chars[i] > '9') {
return false;
}
}
return true;
}
}
3、编程题
(1)编写java程序,输入形式为:Han Shun Ping的人名,以Ping,Han .S的形式打印
出来。其中.S是中间单词的首字母。
(2)例如输入"Willian Jefferson Clinton",输出形式为:Clinton,Willia .j
package com13.exercise;
/**
* @author 甲柒
* @version 1.0
* @title Exercise03
* @package com13.exercise
* @time 2023/1/12 21:36
*/
public class Exercise03 {
public static void main(String[] args) {
String name = "han Shun Ping";
printName(name);
}
public static void printName(String str) {
if (str == null) {
System.out.println("str不能为空");
return;
}
String[] names = str.split(" ");
if (names.length != 3) {
System.out.println("输入的字符串格式不对");
return;
}
String format = String.format("%s,%s .%s", names[2], names[0], names[1].toUpperCase().charAt(0));
System.out.println(format);
}
}
4、编程题
输入字符串,判断里面有多少个大写字母,多少个小写字母,多少个数字
package com13.exercise;
/**
* @author 甲柒
* @version 1.0
* @title Exercise04
* @package com13.exercise
* @time 2023/1/12 21:48
*/
public class Exercise04 {
public static void main(String[] args) {
String str = "6789qq甲柒DDDxyz";
countStr(str);
}
public static void countStr(String str) {
if (str == null) {
System.out.println("输入不能为null");
return;
}
int strLen = str.length();
int numCount = 0;
int lowerCount = 0;
int upperCount = 0;
int otherCount = 0;
for (int i = 0; i < strLen; i++) {
if (str.charAt(i) >= '0' && str.charAt(i) <= '9') {
numCount++;
} else if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z') {
upperCount++;
} else if (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') {
lowerCount++;
} else {
otherCount++;
}
}
System.out.println("数字有" + numCount + "个");
System.out.println("小写字母有" + upperCount + "个");
System.out.println("大写字母有" + lowerCount + "个");
System.out.println("其他字符有" + otherCount + "个");
}
}
5、写出以下运行结果
package com13.exercise;
/**
* @author 甲柒
* @version 1.0
* @title Exercise05
* @package com13.exercise
* @time 2023/1/12 21:59
*/
public class Exercise05 {
public static void main(String[] args) {
String s1 = "甲柒";
Animal a = new Animal(s1);
Animal b = new Animal(s1);
System.out.println(a == b);//false
System.out.println(a.equals(b));//false
System.out.println(a.name == b.name);//true
String s4 = new String("甲柒");
String s5 = "甲柒";
System.out.println(s1 == s4);//false
System.out.println(s4 == s5);//false
String t1 = "hello" + s1;
String t2 = "hello甲柒";
System.out.println(t1.intern() == t2);//true
}
}
class Animal {
String name;
public Animal(String name) {
this.name = name;
}
}