如果您输入太多座位,此程序应该会输出错误。如果我输入无效的席位数:经济部门有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);
}
}
}
== MAIN ==
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.");
}
}
}
答案 0 :(得分:5)
if (economySeats > 0 || economySeats < 9)
这种情况总是正确的(因为任何数字都是正数或小于9),所以当然你没有达到else子句。
您可能需要&&
(和)运算符:
if (economySeats > 0 && economySeats < 9)
您的其他条件也有同样的错误。
答案 1 :(得分:1)
if (economySeats > 0 && economySeats < 9)
因为economySeats
应该大于0且小于9.然后只有条件将跟随else语句。
使用if (economySeats > 0 || economySeats < 9)
时
这意味着economySeat
应该大于0(所以如果你输入大于0的值那么这个条件就是真的)所以,这就是你认为if语句不起作用的原因。
答案 2 :(得分:0)
您的号码范围检查无法按照您的意图运行的原因已经给出。我喜欢暗示你可以通过使用Guava&#39; Range&#39;类型:
Range economySeatsRange = Range.atLeast(1).atMost(8);
//...
boolean inAllowedRange = economySeatsRange.apply(economySeats);
if(inAllowedRange)
// between 1 and 8
else
// signal error
http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Range.html