For循环填充整个而不是一个实例

时间:2014-01-10 17:53:24

标签: java arrays for-loop

我正在创建一个Java预订系统以防止双重预订我创建了一个for循环,它应该在预订完成后将布尔值更改为预订但是当我只想要一个实例时,它会将所有预订更改为预订预订所以没有其他人可以预订。

public static boolean booked;

private void FSubmitActionPerformed(java.awt.event.ActionEvent evt) {  

for ( int i = 0; i < Airplane.Fseat.length; i++)
{
    String seat = FCol.getSelectedItem().toString() + FRow.getSelectedItem().toString();
    String items = Snack.getSelectedItem().toString() + "  " + Drink.getSelectedItem().toString();
    Airplane.Fseat[i] = seat;
    Airplane.item[i] = items;

    if (Airplane.Fseat[i] != null)
    {
        System.out.println("Seat number is First class " + Airplane.Fseat[i].toString() + "\n" +"Food and drink " + " " + Airplane.item[i].toString());
        i++;
    }

    else 
    {
        System.out.println("Cannot book already taken");
    }
}

1 个答案:

答案 0 :(得分:0)

您尚未设置任何条件来检查,如何设置特定座位。

因此,您需要将代码修改为:

    for ( int i = 0; i < Airplane.Fseat.length; i++)
        {

            String seat = FCol.getSelectedItem().toString() + FRow.getSelectedItem().toString();
            String items = Snack.getSelectedItem().toString() + "  " + Drink.getSelectedItem().toString();

        if(your_condition_to_check_if_this_seat_is_selcted ){       

          Airplane.Fseat[i] = seat;
          Airplane.item[i] = items;

         }

            if (Airplane.Fseat[i] != null)
            {
                System.out.println("Seat number is First class " + Airplane.Fseat[i].toString() + "\n" +"Food and drink " + " " + Airplane.item[i].toString());
                i++;
            }

            else 
            {
                System.out.println("Cannot book already taken");
            }
        }