0

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

我有一个名为“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 的问题,尽管一个糟糕的工人会责怪他的工具,我不想成为“那个人”

我还需要做一个“测试课”——以前从未使用过“测试课”,而任务要求我们去看看没人费心去买的“教科书”,我真的不知道!

4

1 回答 1

4

没有switch...while,所以我认为你的问题是一旦你选择了 3,你最终会陷入while(true);无限循环。

正确的伪代码:

do {
   // read System.in

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

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

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();
    }
}
于 2012-05-04T05:32:14.287 回答