我不确定我是否正确执行for循环以在if语句中提供真值。 如果我需要使用这种for循环,我如何增加1。这是我正在努力的部分。
要进行预订,系统会提示用户输入他们希望旅行的目的地和星期几。如果存在此类航班并且航班上有座位,则应输入乘客详细信息并创建新乘客。为航班预订的座位数应增加1.
public void flightBooking(){
Passenger passenger;
String flightDay, flightDestination;
boolean found = false;
Flight myFlight = null;
Scanner scan = new Scanner(System.in);
System.out.println(" On which day do you wish to travel ? ");
flightDay = scan.nextLine();
System.out.println(" What is your destination ? ");
flightDestination = scan.nextLine();
for ( Flight d : flightList )
{
if (d.getDay().equals(flightDay))
{
myFlight= d;
found = true;
}
}
for ( Flight s : flightList )
{
if (s.getDestination().equals(flightDestination))
{
myFlight= s;
found = true;
}
}
if (found == true)
{
System.out.println("The Flight Day and Destination were found, the Flight will be booked.");
Passenger passengers = new Passenger ("Laura", "14 Rathmines Rd ","laura99@gmail.com" , myFlight);
passengerList.add(passengers);
}
else
{
System.out.println("There is no flight booking.");
}
}
答案 0 :(得分:2)
这是优化代码
public void flightBooking(){
Passenger passenger;
String flightDay, flightDestination;
boolean found = false;
Flight myFlight = null;
Scanner scan = new Scanner(System.in);
System.out.println(" On which day do you wish to travel ? ");
flightDay = scan.nextLine();
System.out.println(" What is your destination ? ");
flightDestination = scan.nextLine();
for ( Flight d : flightList )
{
if (d.getDay().equals(flightDay) && d.getDestination().equals(flightDestination))
{
System.out.println("The Flight Day and Destination were found, the Flight will be booked.");
Passenger passengers = new Passenger ("Laura", "14 Rathmines Rd ","laura99@gmail.com" , myFlight);
passengerList.add(passengers);
found = true;
break;
}
}
if (found == false)
{
System.out.println("There is no flight booking.");
}
}
在您的代码中,如果日期不匹配且目的地匹配,那么预订也会发生,因为添加两个for循环正在制作它或条件但它应该是和。此外,它只能通过一个for循环来实现。