一、二维数组+switch,while循环
package net.zx.j2se.day04;
import java.util.Scanner;
public class Text02 {
/*****酒店管理系统******/
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String[][]rooms=new String[6][4];
boolean flag=true;
while(flag){
System.out.println("——————————酒店管理系统———————————");
System.out.println("1、查看房间信息:");
System.out.println("2、办理入住:");
System.out.println("3、办理退房:");
System.out.println("0、退出系统:");
System.out.println("———————————————————————————————");
System.out.print("请选择:");
int n=sc.nextInt();
switch(n){
case 1: //查看房间信息
for(int i=0;i<rooms.length;i++){
for(int j=0;j<rooms[i].length;j++){
//方法一
if(rooms[i][j]!=null){
System.out.print("房间号:"+0+(i+1)+0+(j+1)+" "+rooms[i][j]+"\t");
}else{
System.out.print("房间号:"+0+(i+1)+0+(j+1)+" 空\t");
}
//
}
System.out.println();
}
break;
case 2: //办理入住
System.out.print("请输入房间号:");
int no=sc.nextInt(); //房间号
System.out.print("请输入住客姓名:");
String name=sc.next(); //姓名
int lc=no/100; //楼层
int fj=no%100; //房间
if(lc<1||lc>6||fj<1||fj>4){
System.out.println("请输入正确房间号!");
break;
}
if(rooms[lc-1][fj-1]!=null){
System.out.println("该房间已有客人入住");
break;
}
rooms[lc-1][fj-1]=name;
System.out.println("添加成功~");
break;
case 3: //办理退房
System.out.print("请输入需要退房的房间号:");
int no1=sc.nextInt();
int lc1=no1/100; //楼层
int fj1=no1%100; //房间
if(lc1<1||lc1>6||fj1<1||fj1>4){
System.out.println("请输入正确房间号");
break;
}
if(rooms[lc1-1][fj1-1]==null){
System.out.println("该房间无人入住");
break;
}
rooms[lc1-1][fj1-1]=null; //退房
System.out.println("退房成功!");
break;
case 0: //退出系统
flag=false;
System.out.println("退出系统~");
break;
default:
System.out.println("选择错误,请重新选择!");
}
}
}
}
二、二维数组+方法
package net.zx.j2se.day04;
import java.util.Scanner;
public class Text04 {
/*****酒店管理系统(Find方法)******/
public static void main(String[] args) {
run();
}
public static int printMenu(){ //1、打印菜单
Scanner sc=new Scanner(System.in);
System.out.println("——————————酒店管理系统———————————");
System.out.println("1、查看房间信息:");
System.out.println("2、办理入住:");
System.out.println("3、办理退房:");
System.out.println("0、退出系统:");
System.out.println("———————————————————————————————");
System.out.print("请选择:");
int n=sc.nextInt();
return n;
}
public static void run(){ //2、流程控制
Scanner sc=new Scanner(System.in );
String[][] rooms=new String[6][4];
boolean flag=true;
while(flag){
int n=printMenu();
switch(n){
case 1:
find(rooms);
break;
case 2:
add(rooms);
break;
case 3:
del(rooms);
break;
case 0:
flag=false;
System.out.println("欢迎下次使用~");
break;
default:
System.out.println("输入错误,请重新输入(0~3)");
}
}
}
public static void find(String[][] rooms){ //3.查看房间信息
for(int i=0;i<rooms.length;i++){
for(int j=0;j<rooms[i].length;j++){
if(rooms[i][j]!=null){
System.out.print("房间号:"+0+(i+1)+0+(j+1)+" "+rooms[i][j]+"\t");
}else{
System.out.print("房间号:"+0+(i+1)+0+(j+1)+" 空\t");
}
}
System.out.println();
}
}
public static void add(String[][] rooms){ //4.办理入住
Scanner sc=new Scanner(System.in );
System.out.print("请输入房间号:");
int no=sc.nextInt(); //房间号
System.out.print("请输入住客姓名:");
String name=sc.next(); //姓名
int lc=no/100; //楼层
int fj=no%100; //房间
if(lc<1||lc>6||fj<1||fj>4){
System.out.println("请输入正确房间号!");
return;
}
if(rooms[lc-1][fj-1]!=null){
System.out.println("该房间已有客人入住");
return;
}
rooms[lc-1][fj-1]=name;
System.out.println("添加成功~");
}
public static void del(String[][] rooms){ //5.办理退房
Scanner sc=new Scanner(System.in );
System.out.print("请输入需要退房的房间号:");
int no1=sc.nextInt();
int lc1=no1/100; //楼层
int fj1=no1%100; //房间
if(lc1<1||lc1>6||fj1<1||fj1>4){
System.out.println("请输入正确房间号");
return;
}
if(rooms[lc1-1][fj1-1]==null){
System.out.println("该房间无人入住");
return;
}
rooms[lc1-1][fj1-1]=null; //退房操作
System.out.println("退房成功!");
}
}
结果展示
![](https://i-blog.csdnimg.cn/blog_migrate/4c31c5dfb3f6e731d7f8d4e90bda9b01.png)
![](https://i-blog.csdnimg.cn/blog_migrate/97a0b1fbf6292261a7aa611c7ac7f8f1.png)