一、构建可进可退的多级菜单系统
package net.YGQ.p02.t06;
import java.util.Scanner;
public class XExercise02 {
public static void main(String[] args) {
int mc1;
Scanner sc = new Scanner(System.in);
boolean isRunning = true;
while (isRunning) {
System.out.println(" 名片管理系统");
System.out.println("==================");
System.out.println(" 1. 登录");
System.out.println(" 0. 退出");
System.out.println("==================");
System.out.print("输入菜单编号[1,0]:");
mc1 = sc.nextInt();
switch (mc1) {
case 1:
login();
break;
case 0:
System.out.println("谢谢使用~再见~");
break;
default:
System.out.println("输入的菜单编号有误!");
}
}
}
private static void login() {
System.out.println("执行用户登录功能~");
}
}
private static void login() {
String username,password;
Scanner sc = new Scanner(System.in);
System.out.print("用户名:");
username = sc.next();
System.out.print("密码:");
password = sc.next();
if (username.equals("mike") && password.equals("123456")) {
System.out.println("登录成功");
cardManagement();
} else {
System.out.println("用户名或密码有误,登陆失败");
}
}
private static void cardManagement() {
System.out.println("执行名片管理功能~");
}
}
private static void cardManagement() {
int mc2;
Scanner sc = new Scanner(System.in);
boolean isRunning = true;
while (isRunning) {
System.out.println("=====名片管理======");
System.out.println(" 1. 添加名片");
System.out.println(" 2. 修改名片");
System.out.println(" 3. 查询名片");
System.out.println(" 4. 删除名片");
System.out.println(" 5. 返回上级");
System.out.println("===================");
System.out.print("输入菜单编号[1,2,3,4,5]:");
mc2 = sc.nextInt();
switch (mc2) {
case 1:
System.out.println("执行添加名片功能~");
break;
case 2:
System.out.println("执行修改名片功能~");
break;
case 3:
System.out.println("执行查询名片功能~");
break;
case 4:
System.out.println("执行删除名片功能~");
break;
case 5:
isRunning = false;
break;
default:
System.out.println("输入的菜单编号有误!");
}
}
}