我想調用一種方法來提示用戶輸入英里數,使用加侖數,計算每加侖英里數,顯示此類型汽車在此行程中每加侖行駛多少英里。我也希望這種方法能夠在以後爲每種車型添加一個「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;
}
這是一種想法我去了,但我被困在如何利用陣列的頻率計數器
所以,當你添加'forToyota()'你要重複'forHonda()'中的所有代碼嗎? –
你能解釋更多細節嗎?你在哪裏叫'櫃檯'? –
我目前有多個if(prompt == 1);如果(提示== 2){1代表本田,2代表豐田,並且在那些if中調用類似的方法}我稍後將其更改爲爲計數器切換案例@JimGarrison –