2014-07-26 113 views
-3

如果您輸入的座位太多,此程序應打印一個錯誤。如果我輸入一個無效的座位數:例如經濟部門有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."); 
     }  
    } 
} 

回答

5

if (economySeats > 0 || economySeats < 9)

這個條件總是爲真(因爲任何數字都是正或小於9),所以當然你沒有達到else子句。

你可能想的&&(和)運營商:

if (economySeats > 0 && economySeats < 9)

和你的其他條件都相同的錯誤。

1
if (economySeats > 0 && economySeats < 9) 

由於economySeats應該是大於0且小於然後9.那麼只有條件也會跟着else語句。

當您使用if (economySeats > 0 || economySeats < 9) 這意味着economySeat應該是大於0(所以如果你輸入的值大於0,則該條件將是真實的),所以,這就是爲什麼你在想的if語句心不是工作。

0

爲什麼你的號碼範圍檢查不起作用,因爲你打算已經給出。我howver想暗示,你可以做範圍檢查某種程度上更優雅和利用番石榴的「範圍」型不易出錯:

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