Java中System.out.printf命令的问题 [英] Problem with System.out.printf command in Java
问题描述
我是编程新手,所以我事先道歉。我正在运行一个程序,并且在Java中遇到了 System.out.printf
方法的问题。我知道程序中的其他所有内容都正常工作(测试过)。但是这似乎不起作用。
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.
错误是:
异常在线程mainjava.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) )
java.io.PrintStream.format(未知来源)的
java.io.PrintStream.printf(未知来源)的
Advisor_Score.main的
(Advisor_Score.java:17)
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)
我的代码是:
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.
推荐答案
%d
表示一个整数;你想使用%f
来获得双倍。请参阅格式化字符串语法 Javadoc
%d
represents an integer; you want to use %f
for a double. See the formatting string syntax in the Javadoc
这篇关于Java中System.out.printf命令的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!