2016-04-25 39 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等神奇数字在你的代码中,但只在需要知道它们的地方。