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了
添加回答
举报
0/150
提交
取消