但是今天把switch结构弄明白了,发现上次#1写的登录类还可以再改一改,上次的登陆类里面就是典型的“if里面又一个if里面又一个if”这种,用了switch之后发现确实好读多了。
package com.test;
import java.util.Scanner;
public class NewName {
public static void main(String[] args) {
boolean login = false;
int i = 0;
Scanner input = new Scanner(System.in);
do{
System.out.println("请输入用户名:");
String name = input.next();
System.out.println("请输入密码:");
String password = input.next();
switch(name){
case "Haruki":
if("123456".equals(password)){
System.out.println("账户1登陆成功");
login = true;
System.out.println("现在可以进行数值输入操作");
NumSave.main(args);
}
case "Asakawa":
if("951".equals(password)){
System.out.println("你正在尝试登陆账户2,是否继续?(y or n)");
String tof = input.next();
if("y".equals(tof)){
System.out.println("账户2登陆成功");
Reg();
}else{
System.out.println("你选择了否,终止登陆程序");
System.exit(0);
}
}
default :i++;
System.out.println("登陆信息不合法,剩余尝试次数:"+(5-i)+"次");
if(i == 5){
System.out.println("错误次数累计达到5次,禁止登陆");
System.out.println("终止登陆程序");
System.exit(0);
}
}
}while(i<5 && !login);
}
public static void Reg(){
Scanner reg = new Scanner(System.in);
System.out.println("现在可以选择要进行的操作:");
System.out.println("1:数值输入操作 2:自动求和操作");
System.out.println("输入1或2");
int choose = reg.nextInt();
if(1==choose){
NumSave.save();//调用NumSave类的save方法
}else{
MathTest.Sum();//调用MathTest类的Sum方法
}
}
}