如果您輸入的座位太多,此程序應打印一個錯誤。如果我輸入一個無效的座位數:例如經濟部門有8個座位。但如果我輸入的數字大於此值,程序將不會繼續執行else語句並輸出錯誤消息。程序不會去其他語句
相反,它只是通過if語句並使用無效的座位號進行計算。
import java.util.Scanner;
public class FlightProfit
{
String location;
int TOLEDO_FARE = 200;
int HOUSTON_FARE = 300;
int BOISE_FARE = 400;
int economySeats;
int businessSeats;
int firstClassSeats;
int economySales;
int businessSales;
int firstClassSales;
//int totalSales = economySales + businessSales + firstClassSales;
Scanner in = new Scanner(System.in);
public int addEconomySeats()
{
System.out.print("Enter number of economy seats 1-8: ");
economySeats = in.nextInt();
if(economySeats > 0 || economySeats < 9)
{
return economySeats;
}
else
{
System.out.println("Error: Only 8 seats available in economy.");
}
return 0;
}
public int addBusinessSeats()
{
System.out.print("Enter number of business seats sold: ");
businessSeats = in.nextInt();
if(businessSeats > 0 || economySeats < 7)
{
return businessSeats;
}
else
{
System.out.println("Error: Only 6 seats available in business.");
}
return 0;
}
public int addFirstClassSeats()
{
System.out.print("Enter number of first class seats sold: ");
firstClassSeats = in.nextInt();
if(firstClassSeats > 0 || firstClassSeats < 5)
{
return firstClassSeats;
}
else
{
System.out.println("Error: Only 4 seats available in first class.");
}
return 0;
}
public void getProfit(String loc)
{
location = loc;
if(location.equalsIgnoreCase("toledo"))
{
economySales = economySeats * TOLEDO_FARE;
businessSales = businessSeats * (2 * TOLEDO_FARE);
firstClassSales = firstClassSeats * (4 * TOLEDO_FARE);
System.out.println("Economy Sales: " + economySeats + " seats: " + economySales);
System.out.println("Business Sales: " + businessSeats + " seats: " + businessSales);
System.out.println("First Class Sales: " + firstClassSeats + " seats: " + firstClassSales);
//System.out.println("Total Sales: " + totalSales);
}
else if(location.equalsIgnoreCase("houston"))
{
economySales = economySeats * HOUSTON_FARE;
businessSales = businessSeats * (2 * HOUSTON_FARE);
firstClassSales = firstClassSeats * (4 * HOUSTON_FARE);
System.out.println("Economy Sales: " + economySeats + " seats: " + economySales);
System.out.println("Business Sales: " + businessSeats + " seats: " + businessSales);
System.out.println("First Class Sales: " + firstClassSeats + " seats: " + firstClassSales);
// System.out.println("Total Sales: " + totalSales);
}
else
{
economySales = economySeats * BOISE_FARE;
businessSales = businessSeats * (2 * BOISE_FARE);
firstClassSales = firstClassSeats * (4 * BOISE_FARE);
System.out.println("Economy Sales: " + economySeats + " seats: " + economySales);
System.out.println("Business Sales: " + businessSeats + " seats: " + businessSales);
System.out.println("First Class Sales: " + firstClassSeats + " seats: " + firstClassSales);
//System.out.println("Total Sales: " + totalSales);
}
}
}
== ==主要
import java.util.Scanner;
public class Aviation
{
public Aviation()
{
}
public static void main(String[] args)
{
int option;
FlightCost newFlight = new FlightCost();
FlightProfit flight = new FlightProfit();
Scanner in = new Scanner(System.in);
System.out.print("Enter new flight location: ");
String location = in.next();
do{
String menu = "\n Please select an option to perform"
+ "\n1 (1) Get flight costs."
+ "\n2 (2) Get flight profits."
+ "\n3 (3) Enter names/meals."
+ "\n4 (4) Exit.";
System.out.println(menu);
option = in.nextInt();
}while (option < 0 || option > 4);
switch(option)
{
case 1:
newFlight.getCost(location);
break;
case 2:
flight.addEconomySeats();
flight.addBusinessSeats();
flight.addFirstClassSeats();
flight.getProfit(location);
break;
case 3:
//newFlight.flyers();
break;
case 4:
System.out.println("Exit");
break;
default:
System.out.println("Error: must select menu option.");
}
}
}