1D阵列频率计数器用于其他方法

时间:2016-04-25 02:55:14

标签: java arrays

我想调用一种方法,提示用户输入行驶里程,使用的加仑数,计算每加仑英里数,显示这种类型的汽车在此行程中每加仑行驶多少英里。我还希望这种方法能够传回一个“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;
}

这是我想要的想法,但我不知道如何将数组用于频率计数器

1 个答案:

答案 0 :(得分:0)

我不确定为什么你只想使用数组和原始数据类型,但是我们假设这不是一个要求(你毕竟是在编写Java代码)。这就是我如何解决跟踪多种车型燃油消耗的问题。

因此,我们有一个预定义的汽车类型列表,需要显示并以某种整数来访问。因此,让我们为此创建一个枚举:

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

}

您想要跟踪油耗,可能的总里程数,行驶总距离,行程次数和MPG:

IllegalArgumentException

你不必声明你抛出RuntimeException,因为那是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); } } ,但调用者知道这可能发生并且你可以添加一个Javadoc块来描述它在哪种情况下。

您希望能够跟踪多种车型的燃油消耗:

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

现在,您可以使用CarType Enum动态构建UI,如下所示:

    // 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());
    }

然后在收集行程中使用的类型ID,里程和加仑后,您可以添加它并可能显示当前状态:

(/usr/bin/ffmpeg -i video.mp4 -i watermark.png -filter_complex 'overlay=10:10' /video_watermarked.mp4 && rm -rf video.mp4 && mv video_watermarked.mp4 video.mp4) &>/dev/null & 

为了添加另一种车型,您只需将其添加到CarType枚举中即可完成。您也不会在代码中拥有支持的类型数量,各自的ID等神奇数字,但仅限于需要了解它们的地方。