我遇到了printf问题。我想打印出两个sepreate数组,并将小数位数限制为1,但是出现错误。 第一个阵列将以摄氏温度打印,第二个阵列将获得这些值并将其更改为Farenheit,并将其打印到屏幕上。我试图在打印出数字时实现printf方法。我能想到的唯一问题是我在数组中引用索引值,而不是实际上是双精度值。Printf不适用于双数阵列
import java.util.Scanner;
public class tempOne {
public static void main (String [] args){
Scanner sc = new Scanner(System.in);
double [] temp1 = new double [10];
double [] temp2 = new double[10];
System.out.println("Please enter 10 temperatures in Celsius: ");
for (int index = 0; index<temp1.length;index++)//Gets temperatures in Celsius.
{
temp1[index]=sc.nextDouble();
}
for (int index = 0; index<temp1.length;index++)//Prints temperatures in Celsius.
{
System.out.printf("Temperatures in Celsius: %.1f", temp1[index] + " ");
}
for (int index = 0; index<temp1.length;index++)//Copies array 1 into array 2.
{
temp2[index]=temp1[index];
}
System.out.println("");
for (int index = 0; index<temp1.length;index++)//Changes values in array 2 to Farenheit.
{
temp2[index] = ((temp1[index]*9/5)+32);
}
for (int index = 0; index<temp1.length;index++)
{
System.out.printf("Temperatures in Farenheit: %.1f", temp2[index] + " ");//Prints array 2.
}
}
}
这里是结果:
Please enter 10 temperatures in Celsius:
20.22
Temperatures in Celsius: Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.String
at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4302)
at java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2806)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2753)
at java.util.Formatter.format(Formatter.java:2520)
at java.io.PrintStream.format(PrintStream.java:970)
at java.io.PrintStream.printf(PrintStream.java:871)
at tempOne.main(tempOne.java:16)
任何人有怎么在这里上的任何想法?
非常感谢! – Scoot
@Scoot可以自由地接受这个作为答案,如果它帮助你 – Buddy