1.常用类练习
package com.logic.homework;
public class Homework01 {
public static void main(String[] args) {
String str = "abcdef";
System.out.println("交换前");
System.out.println(str);
try {
str = reverse(str, 1, 41);
} catch (Exception e) {
System.out.println(e.getMessage());
return;
}
System.out.println("交换后");
System.out.println(str);
}
/*
要求
(1)将字符串中指定部分进行反转。比如将"abcdef"反转为"aedcbf'
(2)编写方法 public static String reverse(String str,int start,int end)搞定
思路分析
(1)先把方法定义确定
(2)把String转为char数组 char[] char[]数组的元素是可以交换的
(3) 画出分析示意图
(4) 代码实现
*/
public static String reverse(String str, int start, int end) {
//对输入的参数做一个验证
//重要的编程思想
//1.先写出正确的情况
//2.取反即可
if (!(str != null && start >= 0 && start < end && end < str.length())) {
throw new RuntimeException("参数不正确");
}
char[] arr = str.toCharArray();
char temp = ' ';
for (int i = start, j = end; i < j; i++, j--) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
return new String(arr);
}
}
package com.logic.homework;
/**
* @author logic
* @version 1.0
*/
/*
输入用户名、密码、邮箱,如果信息录入正确,则提示注册成功,否则生成异常对象
要求:
(1)用户名长度为2或3或4
(2)密码的长度为6,要求全是数字 isDigital
(3)邮箱中包含@和.并且@在.的前面
*/
public class Homework02 {
public static void main(String[] args) {
String name = "jack";
String password = "123456";
String email = "logic@outlook.com";
try {
userRegister(name, password, email);
System.out.println("恭喜你注册成功...");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
//(1) 先编写方法 userRegister(String name, String pwd, String email) {}
//(2) 针对 输入的内容进行校核,如果发现有问题,就抛出异常,给出提示
//(3) 单独的写一个方法,判断 密码是否全部是数字字符 boolean
public static void userRegister(String username, String password, String email) {
if (username == null || password == null || email == null) {
throw new RuntimeException("姓名密码和邮箱信息不能为空...");
}
//第一关
int userLength = username.length();
if (!(userLength >= 2 && userLength <= 4)) {
throw new RuntimeException("名字在2-4个字符之间");
}
//第二关
if (!(password.length() == 6 && isDigital(password))) {
throw new RuntimeException("密码长度需为6且均为数字");
}
//第三关
int i = email.indexOf("@");
int j = email.indexOf(".");
if (!(i > 0 && j > i)) {
throw new RuntimeException("邮箱中包含@和.并且@在.的前面");
}
}
//单独的写一个方法,判断 密码是否全部是数字字符 boolean
public static boolean isDigital(String password) {
char[] charArray = password.toCharArray();
for (int i = 0; i < charArray.length; i++) {
if (charArray[i] < '0' || charArray[i] > '9') {
return false;
}
}
return true;
}
}
package com.logic.homework;
/**
* @author logic
* @version 1.0
*/
/*
编写java程序,输入形式为: Lu Ming Fei的人名,以Fei,Lu .M的形式打印出来
其中.M是中间单词的首字母。
例如输入
"Willian Jefferson Clinton"
输出形式为:Clinton, Willian .J
*/
public class Homework03 {
public static void main(String[] args) {
String name = "Lu Ming Fei";
printName(name);
}
/**
* 思路分析
* (1) 对输入的字符串进行 分割split(" ")
* (2) 对得到的String[] 进行格式化String.format()
* (3) 对输入的字符串进行校验即可
*/
public static void printName(String str) {
if (str == null) {
System.out.println("名字不能为空");
return;
}
String[] names = str.split(" ");
if (names.length != 3) {
System.out.println("输入的名字格式不对");
return;
}
String format = String.format("%s,%s .%c", names[2], names[0],
names[1].toUpperCase().charAt(0));
System.out.println(format);
}
}
package com.logic.homework;
/**
* @author logic
* @version 1.0
*/
public class Homework04 {
public static void main(String[] args) {
String str = "AAAA aaaa 1234";
countStr(str);
}
/*
输入字符串 判断里面有多少个大写字母 多少个小写字母 多少个数字
思路分析:
(2) 遍历字符串,如果 char 在 '0'~'9' 就是一个数字
(2) 如果 char 在 'a'~'z' 就是一个小写字母
(3) 如果 char 在 'A'~'Z' 就是一个大写字母
(4) 使用三个变量来记录 统计结果
*/
public static void countStr(String str) {
if (str == null) {
System.out.println("字符串不能为空");
return;
}
int numCount = 0;
int lowerCount = 0;
int upperCount = 0;
int otherCount = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) >= '0' && str.charAt(i) <= '9') {
numCount++;
} else if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z') {
lowerCount++;
} else if (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') {
upperCount++;
} else {
otherCount++;
}
}
System.out.println("数字有: " + numCount);
System.out.println("小写字母有: " + lowerCount);
System.out.println("大写字母有: " + upperCount);
System.out.println("其他符号有: " + otherCount);
}
}