我在使用Java中的printf()时遇到了问题。

huangapple go评论79阅读模式
英文:

I am having trouble with printf() in Java

问题

//Set variables for calculations and perform arithmetic
int sum = num1 + num2 + num3;
double average = (double)sum/3;
int product = num1 * num2 * num3;
int smallest;
int largest;

System.out.printf("The sum is: %d\n", sum);
System.out.printf("The product is: %d\n", product);
System.out.printf("The average is: %.2f\n", average);

The error is as follows:

The sum is: Exception in thread "main" 10
The product is: 18
The average is: java.util.IllegalFormatConversionException: d != java.lang.Double
	at java.base/java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4426)
	at java.base/java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2938)
	at java.base/java.util.Formatter$FormatSpecifier.print(Formatter.java:2892)
	at java.base/java.util.Formatter.format(Formatter.java:2673)
	at java.base/java.io.PrintStream.format(PrintStream.java:1209)
	at java.base/java.io.PrintStream.printf(PrintStream.java:1105)
	at Assignment_1.main(Assignment_1.java:49)
英文:

I am working on a Java assignment and I have written this simple code before; however, our professor this semester is more interested in how we format the code than whether it just works or not. I have 2 statements using printf that function properly, but the 3rd one does not. I changed it to a println statement with the average and it prints fine, but I need to use the printf for this assignment. If I comment out the printf for average, then the first 2 printf statements run without error.

		//Set variables for calculations and perform arithmetic
		int sum = num1 + num2 + num3;
		double average = (double)sum/3;
		int product = num1 * num2 * num3;
		int smallest;
		int largest;
		
		System.out.printf("The sum is: %d\n", sum);
		System.out.printf("The product is: %d\n", product);
		System.out.printf("The average is: %d\n", average);

The error is as follows:

The sum is: Exception in thread "main" 10
The product is: 18
The average is: java.util.IllegalFormatConversionException: d != java.lang.Double
	at java.base/java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4426)
	at java.base/java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2938)
	at java.base/java.util.Formatter$FormatSpecifier.print(Formatter.java:2892)
	at java.base/java.util.Formatter.format(Formatter.java:2673)
	at java.base/java.io.PrintStream.format(PrintStream.java:1209)
	at java.base/java.io.PrintStream.printf(PrintStream.java:1105)
	at Assignment_1.main(Assignment_1.java:49)

答案1

得分: 1

使用 System.out.printf("The average is: %f\n", average);

而不是 System.out.printf("The average is: %d\n", average);

因为double类型的变量格式是 %f%d用于整数)

println 能正常工作是因为它会自动格式化变量。

英文:

Use System.out.printf("The average is: %f\n", average);

Instead of System.out.printf("The average is: %d\n", average);

Because the format of a double variable is %f (%d is for an integer)

println worked correctly because it automatically formats variables.

答案2

得分: 1

因为你的平均值是双精度浮点数而不是整数,所以出现了这个问题。当你尝试打印平均值时,你使用了%d,这是用于整数值的转换说明符。将它改为%f可能会解决你的问题。

英文:

It happens because your average is a double, not a integer. When you try to print average, you are using %d and it is a conversion specifier for int values. Change it to %f might resolve your problem.

huangapple
  • 本文由 发表于 2020年8月26日 09:14:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63589140.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定