gpt4 book ai didi

java - 其他方法的一维阵列频率计数器

转载 作者:太空宇宙 更新时间:2023-11-04 12:41:00 25 4
gpt4 key购买 nike

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

这就是我想要的想法,但我陷入了如何利用频率计数器的数组

最佳答案

我不确定为什么您只想使用数组和原始数据类型,但我们假设这不是必需的(毕竟您正在编写 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 block 来描述在什么情况下会发生这种情况是件好事。

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

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 等,但仅在需要了解它们的地方。

关于java - 其他方法的一维阵列频率计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36831319/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com