日期:2014-05-20 浏览次数:20675 次
int seconds = console.nextInt(); int minutes = seconds/60;//计算出分钟数 if (second%60 != 0) minutes++; //如果不满整分钟,那么要补成整分钟,否则190秒就被当成整3分钟计算了 if (minutes<=3) { pay = 0.2d; } else { pay = 0.2d + (minutes-3)*0.1d; }
------解决方案--------------------
处理小数用BigDecimal
import java.math.BigDecimal; import java.util.Scanner; public class Day03txtHomework3 { public static void main(String[] args) { final int defaultSecond = 3 * 60; final BigDecimal defaultPay = new BigDecimal("0.2"); final BigDecimal extraPay = new BigDecimal("0.1"); //final double defaultPay = 0.2d; //final double extraPay = 0.1d; Scanner console = new Scanner(System.in); System.out.print("请输入秒数:");// 从控制台输入一个秒数 int seconds = console.nextInt(); BigDecimal pay; if(seconds <= defaultSecond) pay = defaultPay; else { int extraTime = (seconds - defaultSecond) / 60 + 1;//注意这里加1 pay = defaultPay.add(extraPay.multiply(new BigDecimal(String.valueOf(extraTime)))); } System.out.println("收费=" + pay.toString() +"元"); } }
------解决方案--------------------
int extraTime = (seconds - defaultSecond) / 60 + 1;//注意这里加1
LS这里不能直接+1,要判断能否被60整除,比如240秒,算下来是4分钟,也就是0.2+0.1费用,如果不判断内否被60整除就+1,那么变成extraTime=2分钟,这是不对的
------解决方案--------------------