我有一个矢量:
p[0]=0.40816269
p[1]=0.37576407
p[2]=0.16324950
p[3]=0.05282373
Run Code Online (Sandbox Code Playgroud)
我需要以4位小数的形式打印矢量值作为百分比.我试过了:
for(int i=0; i<p.length;i++)
{
System.out.printf("Criterion "+i+" has the weigth=%.4f \n" , p[i]*100);
}
Run Code Online (Sandbox Code Playgroud)
这给了我:
Criterion 0 has the weigth=40.8163, .....
Run Code Online (Sandbox Code Playgroud)
但我想打印:
Criterion 0 has the weigth=40.8163 %, .....
Run Code Online (Sandbox Code Playgroud)
我不能在每行的末尾添加符号"%".如果我尝试:
System.out.printf("Criterion "+i+" has the weigth=%.4f %\n" , p[i]*100);
Run Code Online (Sandbox Code Playgroud)
要么:
System.out.printf("Criterion "+i+" has the weigth=%.4f "+"%\n" , p[i]*100);
Run Code Online (Sandbox Code Playgroud)
程序引发异常.先感谢您!
你需要的逃犯%用%%:
System.out.printf("Criterion "+i+" has the weigth=%.4f %%\n" , p[i]*100);
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请参阅java.util.Formatter转换规范