Java 用于其他方法的1D阵列频率计数器
我想调用一个方法,提示用户输入行驶的英里数,使用的加仑数,计算每加仑的英里数,显示这种车在这次旅行中每加仑行驶的英里数。我还希望这个方法传递回一个“1”,以便在以后为每种类型的汽车添加频率计数器。(如果汽车是本田,请在arrayname[1]中添加一个“1”;如果汽车是丰田,请在arrayname[2]中添加一个“1”,以此类推)Java 用于其他方法的1D阵列频率计数器,java,arrays,Java,Arrays,我想调用一个方法,提示用户输入行驶的英里数,使用的加仑数,计算每加仑的英里数,显示这种车在这次旅行中每加仑行驶的英里数。我还希望这个方法传递回一个“1”,以便在以后为每种类型的汽车添加频率计数器。(如果汽车是本田,请在arrayname[1]中添加一个“1”;如果汽车是丰田,请在arrayname[2]中添加一个“1”,以此类推) public static void forHonda(){ double miles, gallons, mpg; miles = Doubl
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;
}
publicstaticvoidforhonda(){
双英里,加仑,英里/加仑;
miles=Double.parseDouble(JOptionPane.showInputDialog(null,“输入行驶里程”);
如果(miles我不确定为什么只使用数组和基本数据类型,但让我们假设这不是一个要求(毕竟您正在编写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);
}
}
然后在收集类型的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 enum中,就完成了。您的代码中也没有神奇的数字,例如您支持的类型数、它们各自的ID等,但只在需要了解它们的地方。我不确定您为什么只想使用数组和基本数据类型,但让我们假设这不是一个要求(毕竟您正在编写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);
}
}
然后在收集类型的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 enum中,就完成了。您的代码中也没有神奇的数字,例如您支持的类型数、它们各自的ID等,但只有在需要了解它们的地方。因此,当您添加forToyota()时
你要重复当前在forHonda()中的所有代码吗?
你能解释更多细节吗?你在哪里调用了计数器
?我现在有多个if(prompt==1);if(prompt==2){1是本田,2是丰田,并在那些if中调用类似的方法}稍后我会将其更改为switch case@jimgarrison。对于计数器,我想在用户选择车型时调用它,并统计他们选择的车型以及次数。如果这有意义的话。这是我主要混淆的部分;当添加forToyota()时,在哪里调用它@ManhLeSo
你要重复当前在forHonda()中的所有代码吗?
你能解释更多细节吗?你在哪里调用了计数器
?我现在有多个if(prompt==1);if(prompt==2){1是本田,2是丰田,并在那些if中调用类似的方法}稍后我会将其更改为switch case@jimgarrison。对于计数器,我想在用户选择车型时调用它,并计算他们选择的车型以及次数。如果这有意义的话。这是我主要困惑的部分;在哪里调用它@ManhLe