gpt4 book ai didi

java - 奇怪的错误 - 从菜单中断后不执行任何操作

转载 作者:行者123 更新时间:2023-12-01 19:05:02 25 4
gpt4 key购买 nike

这是为了类(class)作业。我已经构建了整个程序,除了这一件事之外,它做的一切都是正确的。

我有一个名为“Schedule”的类,此方法位于时间表的最后:

public void bookSeatMenu()
{ boolean leaveBookSeatMenu = false;
String seatBookingMenuStr;
int seatBookingMenuInt = 14;
boolean isInteger = false;
Scanner input = new Scanner(System.in);
System.out.println("Press 1 to add an individual booking, 2 to cancel a booked seat or 3 to go back");

seatBookingMenuStr = input.nextLine();
try {
seatBookingMenuInt = Integer.parseInt(seatBookingMenuStr);
isInteger = true;
}

catch (NumberFormatException e) {

}
switch (seatBookingMenuInt) {
case 1:
bookSeat();
break;
case 2:
cancelSeat();
break;
case 3:
leaveBookSeatMenu = true;
break;
default:
System.out.println("Invalid Choice");
} while (leaveBookSeatMenu == false);

}

我知道你们都知道切换菜单是什么样子,但我想我还是把它放在那里,以防万一(请原谅双关语)我在这里出错了。

继续,我有 bookSeat 方法,这是用户预订座位的地方(效果很好)。然后它会显示 bookSeatMenu() 只是显示菜单。但之后就不会再回到上一个了。

   public void bookSeat()
{
Scanner input = new Scanner(System.in);
boolean isSeatBooked = true;
showSeatPlan();
int seatNum = 0;
int rowNum = 90;
int columnNum = 16;
boolean isInt = false;

while (isSeatBooked == true)
{
System.out.println("Please pick column of a seat to book");
columnNum = input.nextInt();

System.out.println("Please pick row of a seat to book");
rowNum = input.nextInt();


seatNum = (columnNum + ((rowNum) * 15));

if (seats[seatNum] == false)
{
isSeatBooked = false;
}
else
{
System.out.println("This seat is already booked");
}
}

seats[seatNum] = true;


System.out.println("");
bookSeatMenu();
}

现在,无论是出于爱还是金钱,我都无法让它在预订座位后返回到上一个菜单。

大致流程是:

预订座位 --> 进入 bookSeatMenu --> 按 4 返回 --> 到达上一个菜单。

如果我没有预订座位,程序会很高兴地先返回到菜单,但之后,它只是继续在命令提示符中进入新行,不执行任何其他操作,没有错误等.

我很想说这可能是 BlueJ 的问题,尽管坏 worker 会责怪他的工具,而且我不想成为“那个人”

我还需要做一个‘测试类’——以前从来没有用过‘测试类’,而且作业让我们看没人费心去买的‘课本’,我其实不知道!

p>

最佳答案

没有 switch...while 所以我假设你的问题是一旦你选择 3,你最终会进入 while(true); 这是一个无限循环。

正确的伪代码:

do {
// read System.in

// handle menu options with your switch
} while(...)

顺便说一句,设计很糟糕,恕我直言,您应该尝试考虑您的模型(在您的情况下,我会看到类似 RoomSeat SchedulerMenu)并使这些对象彼此交互:

public class Room {
private Seat[][] seats;
public String toString() {
// like showSeatPlan() using toString() of Seat
}
}

public class Seat {
private int row, column;
private boolean isBooked;
public void book() { /* ... */ }
public void cancel() { /* ... */ }
public String toString() { /* "X" or " " */ }
}

public final class Scheduler {
// "main class" with a "main" method
}

public class Menu {
private Room room;
public String toString() {
// print out menu
}
public void bookSeat() { /* ... */ }
public void cancelSeat() { /* ... */ }
}

(类似的东西)

对于测试部分,每个类都有一个测试类,每个方法都有一个测试方法,以Seat为例:

public class Seat {
public void book() {
if (this.isBooled) {
throw new CannotBookException("seats is taken!");
}
this.isBooled = true;
}
}

public class SeatTest {
@Test // when I book a seat, it's markedas booked.
public void testBook() {
final Seat seat = new Seat();
seat.book();
assertTrue(seat.isBooked)
}

@Test(expected = CannotBookException.class) // when I book an already booked seat, I get an exception.
public void testBookAlreadBooked() {
final Seat seat = new Seat();
// book the seat
seat.book();
assertTrue(seat.isBooked)

// try to book again
seat.book();
}
}

关于java - 奇怪的错误 - 从菜单中断后不执行任何操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10442952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com