我是Java新手,我尝试编写一个简单的房间预订程序。
这是我的计划:
将会有一个名为Booking的课程。它将要求用户输入房间ID来预订房间,用户可以按x终止程序。
我想要另一个名为RoomDB的课程。我将使用arraylist来存储房间ID。
我想我也需要构造函数,所以我有另一个名为Room的类。
我的想法是,当程序运行时,它将显示可供用户选择的房间ID。如果用户输入了错误的房间ID,程序将显示错误消息并要求用户再次输入。
如果用户输入了正确的房间ID并且房间ID可用,它将向用户显示成功消息。
如果用户输入了正确的房间ID且房间ID已经预订,则会要求用户再次输入。
这就是我的想法,我开始编写代码。
这是我的三个类的代码
Booking.java
package test;
import java.util.Scanner;
public class Booking
{
static RoomDB roomDB = new RoomDB();
public static void main(String[] args) {
String roomID;
Room room;
Scanner inputID = new Scanner(System.in);
while(true){
System.out.println("Please choose the room you want to book \n"+getRoomList(roomDB));
while(true){
roomID = inputID.nextLine();
if(roomID.equals ("x")){
break;
}
if(getRoom(roomID) == null){
System.out.println("The room ID is incorrect, please enter again or enter x to quit");
}
else{
room = getRoom(roomID);
if(!room.isBooked()){
System.out.println("Book successfully");
break;
}
else{
System.out.println("please enter the room ID again or enter x to quit");
}
}
}
}
}
public static String getRoomList(RoomDB roomDB)
{
String roomList = "";
for(Room r:roomDB.getRoom())
{
if(!r.isBooked())
{
roomList = roomList+r.getRoomID()+" is free"+" ";
}
else
{
roomList = roomList+r.getRoomID()+" has been booked ";
}
}
return roomList;
}
public static Room getRoom(String roomID){
for(Room r:roomDB.getRoom()){
if(r.getRoomID()==roomID){
return r;
}
}
return null;
}
}
RoomDB.java
package test;
import java.util.ArrayList;
public class RoomDB
{
private ArrayList<Room> room;
private String[] roomID = {"Room1","Room2"};
RoomDB(){
room = new ArrayList<Room>();
for(int i=0;i<roomID.length;i++)
{
addRoom(new Room(roomID[i]));
}
}
public void addRoom(Room addRoom){
room.add(addRoom);
}
public ArrayList<Room> getRoom(){
return room;
}
}
Room.java
package test;
public class Room
{
private String roomID = null;
private boolean booked = false;
Room(String roomID)
{
this.roomID = roomID;
}
String getRoomID()
{
return roomID;
}
void cancel()
{
booked = false;
}
boolean isBooked()
{
return booked;
}
}
我检查过没有语法错误,但程序无法正常运行。
问题是程序一直要求用户输入房间ID,即使房间ID是正确的。例如,用户输入Room1,程序仍然要求输入。
我想知道哪个班级会导致问题。
如果有人能就此问题提出建议,我将不胜感激。
答案 0 :(得分:1)
这是完美答案,看到这只是复制它并运行下面的代码,请根据需要相应地删除修改到您的包/类名称,
忘记在分配房间后设置为真,如果房间似乎是免费的,那么分配并将其标记为BOOKED-&gt; true。
package roombooking;
import java.util.ArrayList;
import java.util.Scanner;
class Room
{
private String roomID = null;
private boolean booked = false;
Room(String roomID)
{
this.roomID = roomID;
}
String getRoomID()
{
return roomID;
}
void cancel()
{
booked = false;
}
boolean isBooked()
{
return booked;
}
public void setBooked(boolean booked) {
this.booked = booked;
}
}
class RoomDB
{
private ArrayList<Room> room;
private String[] roomID = {"Room1","Room2"};
RoomDB(){
room = new ArrayList<Room>();
for(int i=0;i<roomID.length;i++)
{
addRoom(new Room(roomID[i]));
}
}
public void addRoom(Room addRoom){
room.add(addRoom);
}
public ArrayList<Room> getRoom(){
return room;
}
}
public class Booking {
static RoomDB roomDB = new RoomDB();
public static void main(String[] args) {
String roomID;
Room room;
Scanner inputID = new Scanner(System.in);
while(true){
System.out.println("Please choose the room you want to book \n"+getRoomList(roomDB));
while(true){
System.out.println("Enter your Room no. : (Enter x for quite ) : ");
roomID = inputID.nextLine();
System.out.println("X : " + roomID.equals("x"));
if(roomID.equals("x")){
System.out.println("Break");
break;
}
if(getRoom(roomID) == null){
System.out.println("The room ID is incorrect, please enter again or enter x to quit");
}
else{
room = getRoom(roomID);
if(!room.isBooked()){
System.out.println("Book successfully");
room.setBooked(true);
break;
}
else{
System.out.println("please enter the room ID again or enter x to quit");
}
}
}
}
}
public static String getRoomList(RoomDB roomDB)
{
String roomList = "";
for(Room r:roomDB.getRoom())
{
if(!r.isBooked())
{
roomList = roomList+r.getRoomID()+" is free"+" ";
}
else
{
roomList = roomList+r.getRoomID()+" has been booked ";
}
}
return roomList;
}
public static Room getRoom(String roomID){
for(Room r:roomDB.getRoom()){
if(r.getRoomID().equals(roomID)){
System.out.println("r.getRoomID : " + r.getRoomID() + " AND user.roomID :" + roomID);
return r;
}
}
return null;
}
}
已编辑:
在这里,
if(!room.isBooked()){
System.out.println("Book successfully");
room.setBooked(true); // this line does, booked room, after booked once it won't assign to any one else.
break;
}
答案 1 :(得分:0)
public static Room getRoom(String roomID){
for(Room r:roomDB.getRoom()){
if(r.getRoomID().equals(roomID)){ //Old : r.getRoomID()==roomID
return r;
}
}
return null;
}
对于比较字符串,请始终使用等于。
答案 2 :(得分:0)
从表面看,你的问题出现在你将字符串与&#34; ==&#34;进行比较的那一刻。运营商。这是不可能的,因为&#34; ==&#34;运算符尝试识别两个对象是否相等,而不是它们各自的值。您可以在此处获得有关String比较如何工作的更好和完整的解释:How do I compare strings in Java?
作为您问题的快速妥善解决方法,请使用:
if(r.getRoomID().equals(roomID))
{
return r;
}
答案 3 :(得分:0)
我还建议您在相同的情况下比较字符串(在Booking.java中的2个位置):
if (roomID.equalsIgnoreCase("x"))
以及
if (r.getRoomID().equalsIgnoreCase(roomID))
希望有所帮助。