亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

幫忙看下哪錯了?程序應該計算并顯示各桶油每加侖所獲里程數,然后打印到目前為止總平均里程數。

幫忙看下哪錯了?程序應該計算并顯示各桶油每加侖所獲里程數,然后打印到目前為止總平均里程數。

慕粉02102016 2016-10-23 15:19:21
public class Chp5 { public static void main(String[] args) { int totalGallons = 0; int gallons; int totalMiles = 0; int miles; double avg1; double avg2; String input,output; input=JOptionPane.showInputDialog("輸入用油加侖數"); gallons=Integer.parseInt(input); while(gallons!=-1){ output=JOptionPane.showInputDialog("輸入里程數:"); miles=Integer.parseInt(output); totalGallons+=gallons; totalMiles+=miles; avg1=miles/gallons; JOptionPane.showMessageDialog(null, "每加侖所獲得的里程數為"+avg1, "Result",JOptionPane.INFORMATION_MESSAGE); input=JOptionPane.showInputDialog("輸入用油加侖數"); gallons=Integer.parseInt(input); } ? ?avg2=totalMiles/totalGallons; JOptionPane.showMessageDialog(null, "總平均里程數為"+avg2, "Result",JOptionPane.INFORMATION_MESSAGE);}}
查看完整描述

1 回答

?
大咪

TA貢獻785條經驗 獲得超332個贊

package?zzzz;

import?javax.swing.JOptionPane;

public?class?Chp5?{

public?static?void?main(String[]?args)?{

int?totalGallons?=?0;
int?gallons;
int?totalMiles?=?0;
int?miles;
double?avg1;
double?avg2;
String?input,output;
input=JOptionPane.showInputDialog("輸入用油加侖數");
gallons=Integer.parseInt(input);
if(gallons!=-1){????????????????//這里判斷的時候有問題,用if去判斷別用while,用while成了死循環了,所以不會往下執行
output=JOptionPane.showInputDialog("輸入里程數:");
miles=Integer.parseInt(output);
totalGallons+=gallons;
totalMiles+=miles;
avg1=miles/gallons;
JOptionPane.showMessageDialog(null,?"每加侖所獲得的里程數為"+avg1,
"Result",JOptionPane.INFORMATION_MESSAGE);
input=JOptionPane.showInputDialog("請再次輸入用油加侖數");????????//這里我給你加了兩個字
gallons=Integer.parseInt(input);
}
??avg2=totalMiles/totalGallons;
??JOptionPane.showMessageDialog(null,?"總平均里程數為"+avg2,
??"Result",JOptionPane.INFORMATION_MESSAGE);
}
}

輸入的時候也是,里數要大于用油加侖數,負責會異常,因為小數的問題除出來以后都是0.XX,然后在轉為整形后就變成0了

查看完整回答
1 反對 回復 2016-10-23
  • 1 回答
  • 0 關注
  • 1518 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號