这是我的计划。只是一个小章节结束练习。问题是,当我运行它时,只有在我的System.printf中有%f时它才起作用,而在我看来它应该是%d。我的所有变量都是double,Math.ceil返回一个double,所以我非常困惑。有人可以澄清一下吗?
import java.util.Scanner;
public class Excersise {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double enteredHours = 0;
double amountDue = 0;
System.out.printf("Please enter the number of hours parked.");
enteredHours = input.nextDouble();
amountDue = calculateCharges(enteredHours);
System.out.printf("The amount due is %f\n", amountDue); // HERE the line
// that when I
// changed it to
// %f it works,
// but again all
// variables are
// double and
// Math.ceil
// returns a
// double.
}
public static double calculateCharges(double hours) {
double roundedHours = 0;
roundedHours = Math.ceil(hours);
double charges = 2.00;
if (roundedHours > 3.0)
charges = charges + .5 * roundedHours;
if (roundedHours >= 24.0)
charges = 10.00;
return charges;
}
}
答案 0 :(得分:1)
http://docs.oracle.com/javase/tutorial/java/data/numberformat.html
%d是格式化'整数'%f用于格式化浮点数/双精度数。我认为你错误地'%d'代表双重
答案 1 :(得分:0)
%f是浮点变量的格式说明符,而%d用于整数变量。
答案 2 :(得分:0)
%f
用于格式化float / double,%d用于十进制,而java %d
用于显示结果,例如: -
public class Exercise
{
public static void main(String[] args)
{
long number = 30208;
System.out.printf("Number: %d", + number);
}
}
这里我们使用%d
来显示长变量的值