p493
将字符串中指定部分进行翻转
package chapter;
public class reverse {
public static void main(String[] args) {
String str = "abcdef";
str = reverseMethod(str,0,3);
System.out.println(str);
}
public static String reverseMethod(String str, int start, int end) {
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;
}
return new String(chars);
}
}
输入用户名,密码,邮箱,输入正确,则提示注册成功,否则生成异常对象
package chapter;
public class HomeWork {
public static void main(String[] args) {
String name = "asd";
String pwd = "548745";
String email= "asd@sd.com";
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.length()>=2 && name.length()<=4)){
throw new RuntimeException("用户名长度错误");
}
// 密码
if(!(pwd.length()==6 && isDigital(pwd) )){
throw new RuntimeException("密码错误");
}
//邮箱
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 < str.length(); i++) {
if(chars[i] < '0' || chars[i] >'9'){
return false;
}
}return true;
}
}
输入 Tony Smith Willam 输出 Willam Tony W