2016-04-25 27 views
1

我想調用一種方法來提示用戶輸入英里數,使用加侖數,計算每加侖英里數,顯示此類型汽車在此行程中每加侖行駛多少英里。我也希望這種方法能夠在以後爲每種車型添加一個「1」以添加到頻率計數器中。 (如果汽車是本田汽車,爲arrayname [1]添加「1」,如果汽車是豐田汽車,則向arrayname [2]添加「1」等)。1D陣列頻率計數器用於其他方法

 int[] mpgList = new int[5]; // 5 because there are 4 more car types 
    mpgList[0] = 

    do{ 
     prompt = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter" 
      + "\n" 
      + "1 For Honda")); 

     if (prompt == 1) 
     {  
      forHonda(); 


     }; 

......

public static void forHonda(){ 
    double miles, gallons, mpg; 

    miles = Double.parseDouble(JOptionPane.showInputDialog(null,"Enter Miles Driven ")); 
     if (miles <= -1){ 
      JOptionPane.showMessageDialog(null,"Input Is Negative" 
        + "\n" 
        + "Try Again"); 
     miles = Double.parseDouble(JOptionPane.showInputDialog(null,"Enter Miles Driven ")); 
     } 
    gallons = Double.parseDouble(JOptionPane.showInputDialog(null,"Enter Gallons Used ")); 
     if (gallons <= -1){ 
      JOptionPane.showMessageDialog(null,"Input Is Negative" 
        + "\n" 
        + "Try Again"); 
     gallons = Double.parseDouble(JOptionPane.showInputDialog(null,"Enter Gallons Used ")); 
     } 
    mpg = (miles/gallons); 
    if (gallons == 0){ 
     JOptionPane.showMessageDialog(null, "Division by Zero" 
       + "\n" 
       + "Try Again"); 
    miles = Double.parseDouble(JOptionPane.showInputDialog(null,"Enter Miles Driven ")); 
    gallons = Double.parseDouble(JOptionPane.showInputDialog(null,"Enter Gallons Used ")); 
    mpg = (miles/gallons); 
    } 
    JOptionPane.showMessageDialog(null,String.format("MPG for HONDA: %.0f" 
      + "\n", mpg)); 

......

public static void counter(int x[]){ 
    for(int counter = 0; counter< x.length; counter++) 
     x[counter]+=1; 
} 

這是一種想法我去了,但我被困在如何利用陣列的頻率計數器

+0

所以,當你添加'forToyota()'你要重複'forHonda()'中的所有代碼嗎? –

+0

你能解釋更多細節嗎?你在哪裏叫'櫃檯'? –

+0

我目前有多個if(prompt == 1);如果(提示== 2){1代表本田,2代表豐田,並且在那些if中調用類似的方法}我稍後將其更改爲爲計數器切換案例@JimGarrison –

回答

0

我不知道爲什麼你只想使用數組和原始數據類型,但讓我們假設這不是一個請求uirement(畢竟你在編寫Java代碼)。以下是我將如何解決跟蹤多種車型燃料消耗的問題。

因此,我們有一個預定義的汽車類型列表,需要顯示並以某種方式訪問​​某個整數。所以,讓我們創建一個枚舉爲:

public enum CarType { 

    HONDA(1, "Honda"), 
    TOYOTA(2, "Toyota"), 
    ALFA(3, "Alfa Romeo") 
    // ... 
    ; 

    private int id = 0; 
    private String displayName; 

    public static CarType forId(int id) { 
     for (CarType type : CarType.values()) { 
      if (type.id == id) { 
       return type; 
      } 
     } 
     throw new IllegalArgumentException("No car type with number " + id); 
    } 

    private CarType(int id, String displayName) { 
     this.id = id; 
     this.displayName = displayName; 
    } 

    public String getDisplayName() { 
     return displayName; 
    } 

    public int getId() { 
     return id; 
    } 

} 

你想跟蹤油耗,推動英里的可能總數,總行駛距離,旅行和MPG數:

public class Consumption { 

    private double miles = 0; 
    private double gallons = 0; 
    private double mpg = 0; 
    private int numberOfTrips = 0; 

    public void addTrip(double miles, double gallons) throws IllegalArgumentException { 
     if (miles > 0 && gallons > 0) { 
      this.miles += miles; 
      this.gallons += gallons; 
      numberOfTrips++; 
      mpg = this.miles/this.gallons; 
     } else { 
      throw new IllegalArgumentException("Both miles and gallons have to be greater than zero"); 
     } 
    } 

    public double getMiles() { 
     return miles; 
    } 

    public double getGallons() { 
     return gallons; 
    } 

    public double getMpg() { 
     return mpg; 
    } 

    public int getNumberOfTrips() { 
     return numberOfTrips; 
    } 

} 

你不沒必要聲明你拋出了IllegalArgumentException,因爲這是一個RuntimeException,但是調用者知道這可能發生,並且你可以添加一個Javadoc塊來描述它,在這種情況下,它很好。

您希望能夠跟蹤多個車型的油耗:

import java.util.HashMap; 

public class ConsumptionManager { 
    private HashMap<CarType, Consumption> data = new HashMap<>(); 

    public Consumption addTripData(CarType type, double miles, double gallons) throws IllegalArgumentException { 
     if (type == null) { 
      throw new IllegalArgumentException("Car type cannot be null"); 
     } 
     Consumption consumption = data.get(type); 
     if (consumption == null) { 
      consumption = new Consumption(); 
      data.put(type, consumption); 
     } 
     consumption.addTrip(miles, gallons); 

     return consumption; 
    } 

    public Consumption getConsumption(CarType type) throws IllegalArgumentException { 
     if (type == null) { 
      throw new IllegalArgumentException("Car type cannot be null"); 
     } 
     return data.get(type); 
    } 

} 

現在,您可以動態建立一個使用CarType枚舉您的UI像這樣的東西:

for (CarType type : CarType.values()) { 
     // build your UI, e.g. on the console something like: 
     System.out.println(String.format("%d) %s", type.getId(), type.getDisplayName())); 
    } 

然後,在收集類型的ID後,在行程中使用的英里數和加侖數將添加並顯示當前狀態:

// create instance of ConsumptionManager somewhere, possibly in your start-up code: 
    // ConsumptionManager mgr=new ConsumptionManager(); 
    try { 
     Consumption consumption=mgr.addTripData(CarType.forId(id), miles, gallons); 
     // display mpg/number of trips/etc, e.g. on the console 
     System.out.println(String.format("Average range after %d trips: %f", consumption.getNumberOfTrips(),consumption.getMpg())); 
    } catch (Exception e) { 
     // display error to the user, e.g. on the console 
     System.out.println(e.getMessage()); 
    } 

爲了添加另一種車型,您只需將其添加到CarType枚舉中即可。你也沒有像你支持的類型數量,它們各自的ID等神奇數字在你的代碼中,但只在需要知道它們的地方。