构建可进不可退的多级菜单系统
一.构建可进不可退的多级菜单系统
package p02.t03;
import java.util.Scanner;
public class XExercise02 {
public static void main(String[] args) {
int mc1 ;
Scanner sc= new Scanner(System.in);
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() {
String username,password;
Scanner sc =new Scanner(System.in);
System.out.print("用户名:");
username=sc.next();
System.out.print("密码:");
password=sc.next();
if (username.equals("李枝蔓")&& password.equals("0512")){
System.out.println("登录成功");
cardManagement();
} else{
System.out.println("用户名或密码错误,登录失败");
}
}
private static void cardManagement() {
int mc2;
Scanner sc= new Scanner(System.in);
System.out.println("==================");
System.out.println(" 1.添加名片");
System.out.println(" 2.修改名片");
System.out.println(" 3.查询名片");
System.out.println(" 4.删除名片");
System.out.println("==================");
System.out.print("输出菜单编号[1,2,3,4]:");
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;
default:
System.out.println("输入菜单编号有误");
}
}
}
运行结果如下:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0578e87c0fe8d2c1ad18b4293ec859bf.png#pic_center)
二.构建可进可退的多级菜单系统
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a2a6a36a0f730b86fc82d59ab2b0e78f.png#pic_center)
运行结果如下:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/11ff31030a9eff43afbd9725c2a1baac.png#pic_center)
附加任务
一.构建在输出栏填写个人基本信息的程序
package c02.s01.p03;
import java.util.Scanner;
public class Example01 {
public static void main(String[] args) {
String id;
String name;
String gender;
int age;
String hobbies;
Scanner sc=new Scanner(System.in);
System.out.print("学号:");
id=sc.next();
System.out.print("姓名:");
name=sc.next();
System.out.print("性别:");
gender=sc.next();
System.out.print("年龄:");
age =sc.nextInt();
System.out.print("兴趣爱好:");
hobbies=sc.next();
System.out.println("学号:"+id);
System.out.println("姓名:"+name);
System.out.println("性别:"+gender);
System.out.println("年龄:"+age);
System.out.println("兴趣爱好:"+hobbies);
}
}
运行结果如下:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9cb98c4f025dc2da1268d518b3980b78.png#pic_center)