-2
我在編寫一個簡單的程序。它符合,但是在進入所需的測量之後,它就會爆炸。它看起來可能是一個轉換錯誤,但所有的數字都是一個浮點數。計算公式時出現Java錯誤
任何人都可以幫忙嗎?
import javax.swing.JOptionPane;
public class Test
{
public static void main(String[] args)
{
String lengthString, radiusString ;
radiusString = JOptionPane.showInputDialog(null, "Enter the radius of the cylinder. ", JOptionPane.QUESTION_MESSAGE);
float radius = Float.parseFloat(radiusString);
lengthString = JOptionPane.showInputDialog(null, "Enter the length of the cylinder. ", JOptionPane.QUESTION_MESSAGE);
float length = Float.parseFloat(lengthString);
float pi = 3.14f ;
float two = 2f;
float bottomArea = (radius * radius * pi);
float cylinderArea = (two * radius * pi * length) + (two + bottomArea) ;
float volume = (bottomArea * length);
JOptionPane.showMessageDialog(null, String.format("The length of the cylinder is %.02f \n", length +
"The radius of the cylinder is %.02f \n", radius +
"The area of the cylinder is %.02f \n", cylinderArea +
"The volume of the cylinder is %.02f \n", volume));
}
}
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.util.Formatter.format(Formatter.java:2455)
at java.lang.String.format(String.java:2940)
at Test.main(Test.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
選中此項:http://stackoverflow.com/help/mcve – cuihtlauac
'float two = 2f;'真的嗎?! – glglgl
我沒有多少年像你自己編程。不好意思。 – BlueConch