航空座位使用布尔值

时间:2014-04-12 04:02:19

标签: javascript jgrasp

我似乎在这个中期项目中遇到了相当困难的时期。我们可以询问您是希望first class还是coach seating,然后从中扣除哪一个,并提供预订其他座位的选择。

我刚刚在头等舱座位中添加了对话框选项的部分,现在它在" dialogresult ="给我一个解析错误。但我无法理清原因。此外,当它确实有效时,它会成功循环回到开始,从计数中扣除一个座位,然后不再接受用户输入再次预订。

我想我只需要另外一套眼睛,在这里真的很困惑。

import javax.swing.*;
import java.util.*;

class Midterm2 {
 public static void main(String[] args) {
  boolean coachSeats[] = new boolean[10];
  boolean firstSeats[] = new boolean [5];
  int seatsFull = 0,FirstSeatCount,CoachSeatCount;


  for(int i=0;i<10;i++){
     firstSeats[i] = false;
     coachSeats[i] = false;

     do{
        FirstSeatCount = 4;
        CoachSeatCount = 9;
        if(firstSeats[i] == false) FirstSeatCount++;
        if(coachSeats[i] == false) CoachSeatCount++;


        int seatClass = Integer.parseInt((String) JOptionPane.showInputDialog(null, 
           "\n" + FirstSeatCount + " Seats avaliable in First Class "+ 
           "\n" + CoachSeatCount + " Seats avaliable in Coach." +
           "\nPress 1 to reserve a seat in first class" +
           "\nPress 2 to reserve a seat in coach:"));

        if (seatClass == 1){

           if(firstSeats[i] == false)
              firstSeats[i] = true;
           FirstSeatCount = 1;{
           dialogresult = JOptionPane.YES_NO_OPTION; 
           JOptionPane.showConfirmDialog(null, "\nBoarding Pass Details -->" +
                                         "\nSeat number :" + (i+1) +
                                         "\nFirst Class" +
                                         "\nWould you like to reserve another seat", JOptionPane.YES_NO_OPTION);

           if (dialogresult == JOptionPane.YES_OPTION); 
           if(dialogresult == JOptionPane.NO_OPTION) 
              break;
                            }                                                       


        if(FirstSeatCount == 0){
           JOptionPane.showInputDialog ("\nNo seats availabe in this class," + 
                         "would you like to travel in another class(y/n):");
                                    }
                            }

1 个答案:

答案 0 :(得分:1)

你有一个花括号在自己的下一行浮动,需要删除:

FirstSeatCount = 1;{

将其更改为:

FirstSeatCount = 1;

这应解决您的解析问题。 Haven没有尝试编译代码以查看是否存在任何其他问题,但这应该可以解决这个特定问题。

此外,在格式化说明中,请帮自己一个忙,请不要以您当前的方式格式化您的大括号。虽然这是主观的,但对我来说,实际上只有两个好的做法:

if(boolean comparison){
    some code...
}

或我个人最喜欢的:

if(boolean comparison)
{
    some code...
}

我可能会被一些喜欢某种非正统风格的人惹恼或投票,但实际上,除了这两种伤害可读性之外还做其他任何事情,作为编程辅导的人,这真的是一个巨大的痛苦阅读不遵循这两个约定之一的代码的屁股。 / endrant