Java 注册后登录界面程序,异常判断是否为空等,ArrayList,学生类,集合【诗书画唱】
制作注册后登录界面,使用异常判断用户输入是否为空,输入内容长度是否大于6位数,密码是否为空,年龄是否满18(使用异常制作)
package fuXi;
import java.util.Scanner;
public class yichang {
public static void main(String[] args) {
yonghu yh = new yonghu();
while (true) {
System.out.println("请输入你的操作:1.登录 2.注册");
Scanner s = new Scanner(System.in);
int num = s.nextInt();
if (num == 1) {
System.out.println("----执行登录操作----");
System.out.println("请输入用户名:");
String uname = s.next();
try{
if(uname.length()<6){throw new StringIndexOutOfBoundsException("诗书画唱提醒你,"
+ "输入内容长度已经小于6");
}
else{System . out. println("输入内容长度已经不小于6,输入成功!");
}
} catch (StringIndexOutOfBoundsException e) {
System. out. println("诗书画唱提醒你,输入内容长度已经小于6,为确保你的安全,"
+ "最好大于6位数!且密码不能为空!");
}
System.out.println("请输入你的密码");
String pwd = s.next();
System.out.println("请输入你的年龄");
int age = s.nextInt();
try{
if(age<18){throw new Exception("诗书画唱提醒你,你未满18岁,不能玩和购买这款游戏!");
}else{System . out. println("诗书画唱提醒你,年龄设置成功!");}
}
catch (Exception e) {
System. out. println("诗书画唱警告你,你未满18岁!");
}
if (uname.equals(yh.uname) && pwd.equals(yh.pwd)&& age ==yh.age) {
System.out.println("登录成功!");
break;
} else {
System.out.println("登录失败,你未注册,请先注册 ");
}
} else if (num == 2) {
System.out.println("----执行注册操作----");
System.out.println("请输入用户名:");
String uname = s.next();
System.out.println("请输入你的年龄");
int age = s.nextInt();
try{
if(age<18){throw new Exception("诗书画唱提醒你,你未满18岁,不能玩和购买这款游戏!");
}else{System . out. println("诗书画唱提醒你,年龄设置成功!");}
}
catch (Exception e) {
System. out. println("诗书画唱警告你,你未满18岁!");
}
System.out.println("请输入你的密码(最好输入内容长度大于6位数,不能为空)");
String pwd = s.next();
//pwd=null;
try{
if(pwd.length()<6){throw new StringIndexOutOfBoundsException("诗书画唱提醒你,密码不能小于6位数!");
/*拋出异常*/
}
else{System . out. println("诗书画唱提醒你,密码设置成功!");}
} catch (StringIndexOutOfBoundsException e) {
System. out. println("诗书画唱提醒你,输入内容长度不大于6位数,为确保你的安全,"
+ "必须大于6位数!且密码不能为空!");
}
catch (NullPointerException e) {
System. out. println("诗书画唱提醒你,密码不能为空!");
}
System.out.println("因为国家法律,要实名认证,请输入你的真实姓名");
String name = s.next();
yh.uname = uname;
yh.pwd = pwd;
yh.name = name;
yh.age = age;
System.out.println("注册成功!");
}
}
}
}
class yonghu {
String uname;
String pwd;
String name;
int age;
}


用ArrayList:创建一个学生类,包含学生编号,姓名,性别,成绩,将学生放入类集合中,添加5名学生,算出学生的平均成绩
package fuXi;
import java.util.ArrayList;
public class jiHe {
public static void main(String[] arrgs) {
ArrayList s = new ArrayList();
student A1 = new student("点赞编号ID:233", "诗名", '男', 100);
student A2 = new student("点赞编号ID:666", "书名", '男', 100);
student A3 = new student("点赞编号ID:999", "画名", '男', 100);
student A4 = new student("点赞编号ID:888", "唱名", '男', 100);
student A5 = new student("点赞编号ID:20U(哟)~", "帅名", '男', 100);
s.add(A1);
s.add(A2);
s.add(A3);
s.add(A4);
s.add(A5);
double ChengJiZongHe = 0;
for (int i = 0; i < s.size(); i++) {
student k = (student) s.get(i);
ChengJiZongHe += k.chengji;
}
System.out.println("学生的平均成绩:" + ChengJiZongHe / s.size());
}
}
class student {
String ID;
String name;
char sex;
double chengji;
public student(String ID, String name, char sex, double chengji) {
this.ID = ID;
this.name = name;
this.sex = sex;
this.chengji = chengji;
}
}



