public class A04 {
public static void main(String[] args) {
//参数数组
Book[] books = new Book[4];
books[0] = new Book("红楼梦", 100);
books[1] = new Book("金瓶梅新", 90);
books[2] = new Book("青年文摘20年", 5);
books[3] = new Book("java从入门到放弃~", 300);
// (1)price从小到大
Arrays.sort(books, new Comparator
作业
public class A05 {
public static String reverse(String str, int start, int end){
//对输入的参数做一个验证
//重要的编程技巧!!!!
//(1)写出正确的情况,然后取反即可
if(!(str != null && start >= 0 && end > start && end < str.length())){
throw new RuntimeException("参数不正确"); //throw new 运行时异常
}
char[] chars = str.toCharArray();
for(int i = start, j = end; i < j; i++,j--){
char temp = chars[i];
chars[i] =chars[j];
chars[j] = temp;
}
return new String(chars);
}
public static void main(String[] args) {
try {
System.out.println(A05.reverse("abcdef", 1, 42));
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
public class A06 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入用户名:");
String userName = scanner.next();
System.out.println("请输入密码:");
String password = scanner.next();
System.out.println("请输入邮箱:");
String email = scanner.next();
try {
userRegister(userName, password, email);
System.out.println("注册成功!");
} catch (Exception e) {
System.out.println(e.getMessage());
}
scanner.close();
}
public static void userRegister(String userName, String password, String email){
//校验输入是否正确
if(!(userName.length() >=2 && userName.length() <= 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 < email.length()-1 && i < j)){
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;
}
}
public class A07 {
public static void main(String[] args) {
System.out.println(printName("Yu Er Lan"));
}
public static String printName(String str){
if(str == null){
System.out.println("姓名不能为空");
}
String[] names = str.split(" "); //对字符串进行分割(返回的是数组)
if(names.length != 3){
System.out.println("姓名格式不正确: XX XX XX");
}
String format = String.format("%s,%s .%c", names[2],names[0],names[1].toUpperCase().charAt(0));
return format;
}
}
public class A08 {
public static void main(String[] args) {
judge("asAS12@#");
}
public static void judge(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 + "个");
}
}
equals比较两个对象
- 特殊的类:String, File,Data 值
- 一般类:地址



