Java 中 System.out.printf 命令的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3693079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Problem with System.out.printf command in Java
提问by Spencer
I am new to programming so I apologize beforehand. I am running a program and am having a problem with the System.out.printf
method in Java. I know everything else in the program is working correctly (tested it). However this does not seem to work.
我是编程新手,所以我事先道歉。我正在运行一个程序并且System.out.printf
在 Java 中遇到了该方法的问题。我知道程序中的其他所有内容都正常工作(已测试)。然而,这似乎不起作用。
The error is:
错误是:
Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.Double
at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source)
at java.util.Formatter$FormatSpecifier.printInteger(Unknown Source)
at java.util.Formatter$FormatSpecifier.print(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at java.io.PrintStream.printf(Unknown Source)
at Advisor_Score.main(Advisor_Score.java:17)
线程“main”中的异常 java.util.IllegalFormatConversionException: d != java.lang.Double
at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source)
at java.util.Formatter$FormatSpecifier.printInteger(Unknown Source)
at java .util.Formatter$FormatSpecifier.print(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at java.io.PrintStream.printf(Unknown Source)
at Advisor_Score .main(Advisor_Score.java:17)
My code is:
我的代码是:
import java.lang.Math;
public class Advisor_Score {
public static void main(String[] args){
double l[] = {101,1,1,1};
double k[] = {102,2,2,4};
double m[] = {103,5,5,5,5,5,5,5};
double All_users[][]={l,k,m};
double sum[]=new double [All_users.length];
double [] raw_advisor=new double [All_users.length];
double []advisor_score= new double [All_users.length];
for (int i=0;i<All_users.length;i++){
for(int j=1;j<All_users[i].length;j++){
sum[i]+=All_users[i][j];
}
raw_advisor[i]=((sum[i]-(3*(All_users[i].length-1)))/4);
advisor_score[i]= 2.5+(2.5*(1-Math.pow(Math.E, -.5*raw_advisor[i])));
System.out.printf("%d: %d\n", All_users[i][0], advisor_score[i]);
}
}
}
Not really sure why it's not working.
不确定为什么它不起作用。
采纳答案by Michael Mrozek
%d
represents an integer; you want to use %f
for a double. See the formatting string syntaxin the Javadoc
%d
代表一个整数;你想用%f
双打。请参阅Javadoc 中的格式化字符串语法