英文:
How do I format the output decimal to two places after the decimal in java?
问题
我希望我的 fallingTime 数字限制在两位小数。我使用了 "String.format(%.2f)" 但是出现了错误信息。
这是错误信息:
Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.String
at java.base/java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4426)
at java.base/java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2951)
at java.base/java.util.Formatter$FormatSpecifier.print(Formatter.java:2898)
at java.base/java.util.Formatter.format(Formatter.java:2673)
at java.base/java.util.Formatter.format(Formatter.java:2609)
at java.base/java.lang.String.format(String.java:3302)
at fallingDistance.main(fallingDistance.java:22)
public class fallingDistance {
public static double fallDistance(int fallingTime){
double distance;
double g = 9.8;
distance = (0.5*g*Math.pow(fallingTime, 2)); // 修改这一行
return distance;
}
//public static void displayHeader(){
//System.out.println("Time\t\t\t\t\tDistance\n----\t\t\t\t\t--------");
//}
public static void main(String[] args){
//displayHeader();
for(int time = 1; time <= 10; time++){
System.out.println(time + " seconds\t\t" + String.format("%.2f", fallDistance(time)) + " meters"); // 修改这一行
}
}
}
英文:
I want my fallingTime numbers to be limited to two decimal places. I used "String.format(%.2f)" but it is giving me an error message.
This is the error message:
Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.String
at java.base/java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4426)
at java.base/java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2951)
at java.base/java.util.Formatter$FormatSpecifier.print(Formatter.java:2898)
at java.base/java.util.Formatter.format(Formatter.java:2673)
at java.base/java.util.Formatter.format(Formatter.java:2609)
at java.base/java.lang.String.format(String.java:3302)
at fallingDistance.main(fallingDistance.java:22)
public class fallingDistance {
public static double fallDistance(int fallingTime){
double distance;
double g = 9.8;
distance = (0.5*g*Math.pow(fallingTime, fallingTime));
return distance;
}
//public static void displayHeader(){
//System.out.println("Time\t\t\t\t\tDistance\n----\t\t\t\t\t--------");
//}
public static void main(String[] args){
//displayHeader();
for(int time = 1; time <= 10; time++){
System.out.println(time+ " seconds\t\t" + String.format("%.2f", fallDistance(time) + " meters"));
}
}
}
答案1
得分: 1
替换 +
为 ,
System.out.println(time + " seconds\t\t" + String.format("%.2f", fallDistance(time)) + " meters");
英文:
Replace +
with ,
System.out.println(time + " seconds\t\t" + String.format("%.2f", fallDistance(time), " meters"));
答案2
得分: 1
你误放了一个括号。以下应该可以工作。
for (int time = 1; time <= 10; time++) {
System.out.println(time + "秒\t\t" + String.format("%.2f", fallDistance(time)) + "米");
}
英文:
You had misplaced a bracket. The below should work.
for (int time = 1; time <= 10; time++) {
System.out.println(time + " seconds\t\t" + String.format("%.2f", fallDistance(time)) + " meters");
}
答案3
得分: 1
我从代码末尾移除了“ meters”,然后放在“%.2f”之后,这样就可以了。代码现在看起来是这样的:
{System.out.println(time + "秒\t\t" + String.format("%.2f 米", fallDistance(time)));}
英文:
I removed " meters" from the end of the code and put it after "%.2f" and that worked. The code looks like this now:
{System.out.println(time + " seconds\t\t" + String.format("%.2f meters", fallDistance(time)));}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论