剛開始學習java,模擬酒店管理系統的一道題遇到點問題!請大神幫嗎看看一下是哪裏錯了!

如下是一個測試類!java

package 數組.做業3; import java.util.Scanner; public class Test { static Hotel h = new Hotel(); public static void main(String[] args) { System.out.println("《《《《《《《《《《《《歡迎光臨大酒店》》》》》》》》》》》》"); while (true) { Scanner scanner = new Scanner(System.in); System.out.println("1.查看酒店的房間及狀態;2.定房間;3.退房;4.退出系統"); System.out.print("請輸入數字選擇你須要的服務:"); while (true) { if (scanner.hasNextInt()) { int no = scanner.nextInt(); if (no >= 1 && no <= 4) { checkServices(no); System.out.println(); break; } else { System.out.println("輸入錯誤,請從新輸入!"); System.out.println("1.查看酒店的房間的示意圖;2.定房間;3.退房;4.退出系統"); System.out.print("請輸入數字選擇你須要的服務:"); continue; } } else { System.out.println("輸入格式有誤:應輸入數字爲:1~5"); System.out.println("1.查看酒店的房間的示意圖;2.定房間;3.退房;4.退出系統"); System.out.print("請輸入數字選擇你須要的服務:"); scanner.next(); } } } } public static void checkServices(int no) { switch (no) { case 1: //查看酒店的房間及狀態
 h.roomList(); break; case 2: //訂房
 h.reservation(true); break; case 3: //退房
            h.reservation(false); break; case 4: //退出
            System.out.println("期待您下次使用!"); System.exit(0); break; } } }

如下是房間類數組

public class Room { private String no; private String type;//標準間,雙人間,豪華間
    private boolean isUse;
    
    public String getNo() { return no; } public void setNo(String no) { this.no = no; } public String getType() { return type; } public void setType(String type) { this.type = type; } public boolean isUse() { return isUse; } public void setUse(boolean isUse) { this.isUse = isUse; } public Room() { super(); } Room(String no, String type,boolean isUse){ this.no= no; this.type = type; this.isUse = isUse; } public String toString() { return "    【房號】:"+ no + "  【類型】:"+type+"  【狀態】:"+(isUse?" 已入住   ":"  空閒  "); } }

如下是酒店類測試

import java.util.Scanner; /* * 規定酒店:5層,每層10個房間 * 1.2層是標準件 * 3.4層是雙人間 * 5 層是豪華間 */
public class Hotel { Room[][] rooms; //初始化房間
 Hotel() { rooms = new Room[5][10]; for (int i = 0; i < rooms.length; i++) { for (int j = 0; j < rooms[i].length; j++) { if (i == 0 || i == 1) { rooms[i][j] = new Room((i + 1) * 1000 + (j + 1) + "", "標準間", false); } if (i == 2 || i == 3) { rooms[i][j] = new Room((i + 1) * 1000 + (j + 1) + " ", "雙人間", false); } if (i == 4) { rooms[i][j] = new Room((i + 1) * 1000 + (j + 1) + " ", "豪華間", false); } } } } // 對外提供查看房間列表
    public void roomList() { for (int i = 0; i < rooms.length; i++) { for (int j = 0; j < rooms[i].length; j++) { System.out.print(rooms[i][j]); } System.out.println(); } } // 提供對外預約及退房方法
    public void reservation(boolean a) { Scanner scanner = new Scanner(System.in); outer: while (true) { System.out.print("請輸入房間號:"); if (scanner.hasNextInt()) { String no = (scanner.next()); inner: for (int i = 0; i < rooms.length; i++) { for (int j = 0; j < rooms[i].length; j++) { if (rooms[i][j].getNo().equals(no)) { if (a) { if (rooms[i][j].isUse()) { System.out.println("手慢了,房間已經被預約啦!"); continue outer; } else { rooms[i][j].setUse(true); System.out.println("房間訂好啦,祝您入住愉快!"); break outer; } } else { if (rooms[i][j].isUse()) { rooms[i][j].setUse(false); System.out.println("房間已經退了,歡迎下次光臨!"); break outer; } else { System.out.println("房間沒有人入住!"); continue outer; } } } } } System.out.print("沒有這個房間,請查證後從新輸入!"); } else { System.out.println("輸入房間號格式錯誤!"); scanner.next(); } } } }

以上是代碼!this

問題是如今我只能輸入1層和2層的房間號spa

若是想預約3層或以上的房間,就匹配不上了!code

求大神指點一下!blog

相關文章
相關標籤/搜索