【问题标题】:Looping through Arrays in Java在 Java 中循环遍历数组
【发布时间】:2016-05-25 21:32:46
【问题描述】:

我正在尝试编写一个程序来模拟航空公司预订系统。我应该使用布尔类型的数组来表示座位数。前五个座位代表头等舱,后五个座位代表经济舱。最初程序必须允许用户在头等舱和经济舱之间做出选择,然后他的选择被处理如下: 用户只能在他选择的班级中分配一个空座位。 一旦一堂课满了,用户就可以选择转到下一堂课 如果用户同意转到下一班,则会打印一张简单的登机牌。 如果用户拒绝移动到下一个班级。显示下一个航班的时间。我将不胜感激有关如何遍历数组元素以确定其真假的帮助。此外,我试图在用户做出选择之前显示每个班级的可用座位数这是我到目前为止所写的。我的代码远未完成,我承认,我是一个新手程序员,请帮忙。谢谢。

import java.util.Scanner;

public class AirlineReservation 
{ 
private boolean[] seats =;  // array to hold seating capacity
private String AirlineName; // name of airline 
private int[] counter = new int[5]

// constructor to initialize name and seats 
public Airline(String name, boolean[] capacity )
{
    AirlineName = name;
    seats = capacity;
} // end constructor 

// method to set the Airline name
public void setName( String name )
{
    AirlineName = name; // store the course name
} // end method setCourseName

// method to retreive the course name 
public String getName()
{ 
    return AirlineName;
} // end method getName

// display a welcome message to the Airline user 
public void displayMessage()
{
    // display welcome message to the user 
    System.out.printf("Welcome to the Self-Service menu for\n%s!\n\n",
        getName() );
} // end method displayMessage

// processUserRequest 
public void processUserRequest() 
{
    // output welcome message 
    displayMessage();

    // call methods statusA and StatusB 
    System.out.printf("\n%s %d:\n%s %d:\n\n",
        "Number of available seats in First class category is:", statusA(),
        "Number of available seats in Economy is", statusB() );

    // call method choice
    choice(); 

    // call method determine availability
    availability();

    // call method boarding pass
    boardingPass();
} // end method processUserRequest

public int statusA() 
{
    for ( int counter = 0; counter <= (seats.length)/2; counter++ )
} // revisit method 

// method to ask users choice 
public String choice()
{
    System.out.printf(" Enter 0 to select First Class or 1 to select Economy:")
    Scanner input = new Scanner( System.in );
    boolean choice = input.nextBoolean();   
} // end method choice

// method to check availability of user request
public String availability()
{
    if ( input == 0)
        System.out.printf("You have been assigned seat number \t%d", seats[ counter ]);
    else 
        System.out.printf("You have been assigned seat number \t%d", seats[ counter ]);
}
}

【问题讨论】:

    标签: java arrays loops


    【解决方案1】:

    @Anthony :由于您是新手,因此请查看以下 cmets : 1. 提问时要具体。在这种情况下,问题是“如何遍历数组!”。您发布了完整的问题陈述。 2.如果之前有人问过类似的问题,请谷歌或在互联网上适当地研究它。在这种情况下,可以在这里找到类似的问题: Iterate through string array in Java

    由于这些原因,人们对您的查询投了反对票!

    现在只想回答你的问题,因为你对编程很陌生:

        boolean flag[] = {true,false,true,false};
        for(int i=0;i<flag.length;i++){
            System.out.println(flag[i]);
        }
    

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-22
      • 2011-10-21
      • 2014-06-04
      相关资源
      最近更新 更多