switch....case写的界面如何返回上一级

本文介绍了一种基于Java的菜单驱动系统的设计与实现方法。该系统通过多级菜单引导用户进行操作选择,包括登录验证、主菜单展示及各项功能选项。采用循环结构确保用户能够反复进行操作直至选择退出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


1.在每一个switch......case外套一个while循环


import java.util.Scanner;


 class Menu1 {
	void showLoginMenu(){
		System.out.println("\t\t欢迎使用我行我素管理系统");
		System.out.println("\n\t\t1.登录系统");
		System.out.println("\n\t\t2.退出");
		System.out.println("\n**********************************");
	}
	void  showMainMenu(){
		System.out.println("\t\t我行我素购物管理主菜单");
		System.out.println("\n**************************************");
		System.out.println("\n\t\t1.客户信息管理");
		System.out.println("\n\t\t2.真情回顾");
		System.out.println("\n**************************************");
	}
	void showCustMenu(){
		System.out.println("\t\t客户信息管理菜单");
		System.out.println("\n**************************************");
		System.out.println("\n\t\t1.客户信息");
		System.out.println("\n\t\t2.购物金额");
		System.out.println("\n**************************************");
	}
	void showSendMenu(){
		System.out.println("\n\t\t真情回馈菜单");
		System.out.println("\n**************************************");
		System.out.println("\n\t\t1.幸运大放送");
		System.out.println("\n\t\t2.幸运抽奖");
		System.out.println("\n\t\t3.生日问候");
		System.out.println("\n**************************************");
	}
}
 
 public class Menu {

		public static void main(String[] args) {
			int n=0,b=0,c=0,d=0;			
				Menu1 m=new Menu1();
				m.showLoginMenu();
				Scanner input=new Scanner(System.in);
				System.out.print("请选择,输入数字:");
				n=input.nextInt();
				do{
				switch(n){
				   case 1:do {
					   m.showMainMenu();
					   System.out.print("请选择,输入数字或按0返回上一级:");
					   b=input.nextInt();
					   switch(b){
			               case 1:do {
			            	   m.showCustMenu();
			            	   System.out.print("请选择,输入数字或按0返回上一级:");
			            	   c=input.nextInt();
			            	   switch(c){
			                      case 1:System.out.println("客户信息");break;
			                      case 2:System.out.println("购物金额");break;			                      
			                      }
			            }while(c!=0);
		                   break;
			               case 2:do {
			            	   m.showSendMenu();
			                   System.out.print("请选择,输入数字或0返回上一级:");
			                   d=input.nextInt();
			                   switch(d){
			                      case 1:System.out.println("执行幸运大放送");break;
			                      case 2:System.out.println("执行幸运抽奖");break;
			                      case 3:System.out.println("执行生日问候");break;
			                      }
			               }while(d!=0)	;
			               break;
			          }				   					   
				   }while(b!=0);	
				   break;
				   case 2:System.out.println("退出");
				}	
			}while(n!=2);
		}
 }
 

 

2.套一个while循环

package day05Z;

import java.util.Scanner;

 class StartSMS1 {
	void show(){
		System.out.println("\t\t欢迎使用我行我素管理系统");
		System.out.println("\n\t\t1.登录系统");
		System.out.println("\n\t\t2.退出");
		System.out.println("\n**********************************");
	}
}
public class StartSMS {
	public static void main(String[] args) {
		StartSMS1 s=new StartSMS1();
		String s1,s2;
		boolean bool=true;
		while(bool){
			s.show();
			Scanner input=new Scanner(System.in);
			System.out.print("请选择,输入数字:");
			int a=input.nextInt();
		    switch(a){
		    case 1:System.out.print("请输入用户名:");
		          s1=input.next();
		    		System.out.print("请输入密码:");
		    		s2=input.next(); 
		    		if(s1.equalsIgnoreCase("admin")&&s2.equalsIgnoreCase("0000")){
		    			System.out.println("@@登陆成功:admin@@");
		    			bool=false;
		    		}else{
		    			System.out.println("@@您没有权限进入系统,请重新登陆。@@");	    			
		    		}
		    		break;
		    case 2:System.out.println("退出");
		           break;
		}
		System.out.println();
		}
	}
}

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值