【问题标题】:1D array frequency counter for other methods其他方法的一维阵列频率计数器
【发布时间】:2016-04-25 02:55:14
【问题描述】:

我想调用一个方法,提示用户输入行驶里程、使用的加仑数、计算每加仑英里数、显示这种类型的汽车在这次旅行中每加仑行驶了多少英里。我还希望这种方法能够回传一个“1”,以便稍后为每种类型的汽车添加频率计数器。 (如果汽车是本田,则在 arrayname[1] 中添加“1”,如果汽车是丰田,则在 arrayname[2] 中添加“1”等)。

     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;
}

这是我想要的一种想法,但我被困在如何将数组用于频率计数器

【问题讨论】:

  • 那么,当您添加forToyota() 时,您是否要重复当前forHonda() 中的所有代码?
  • 您能详细解释一下吗?你在哪里打电话给counter
  • 我目前有多个 if (prompt ==1); if (prompt ==2) {1 是本田,2 是丰田,并在 if 中调用类似的方法} 我稍后将其更改为 switch case @JimGarrison
  • 对于计数器,id 想在用户选择汽车类型时调用它,并计算他们选择的汽车类型以及多少次......如果这有意义的话。这是我主要困惑的部分;在哪里称呼它@ManhLe

标签: java arrays


【解决方案1】:

我不确定您为什么只想使用数组和原始数据类型,但我们假设这不是必需的(毕竟您正在编写 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 Enum 动态构建您的 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 等,而只是在需要了解它们的地方。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    • 1970-01-01
    • 2021-11-01
    相关资源
    最近更新 更多