获取线程中的错误异常";“主要”;java.util.IllegalFormatConversionException:d!=java.lang.String
我遇到了这个错误“线程中的异常”main“java.util.IllegalFormatConversionException:d!=java.lang.String”。我发现这在Java新手中是一个相当常见的问题,虽然我尝试将建议应用到我的代码中,但我没有取得任何成功。我希望能得到一些反馈和建议,如何让这一切正常运行。我可以使用println使其正常工作,但赋值需要格式化输出。如有任何建议,将不胜感激。 谢谢获取线程中的错误异常";“主要”;java.util.IllegalFormatConversionException:d!=java.lang.String,java,Java,我遇到了这个错误“线程中的异常”main“java.util.IllegalFormatConversionException:d!=java.lang.String”。我发现这在Java新手中是一个相当常见的问题,虽然我尝试将建议应用到我的代码中,但我没有取得任何成功。我希望能得到一些反馈和建议,如何让这一切正常运行。我可以使用println使其正常工作,但赋值需要格式化输出。如有任何建议,将不胜感激。 谢谢 import javax.swing.JOptionPane; public
import javax.swing.JOptionPane;
public class UserIO {
public static void main(String[] args) {
// initialize coefficients.
double a;
double b;
double c;
// int counter = 0;
String userInput; // take a String for input.
// display message. returns null.
JOptionPane.showMessageDialog(null,
"Welcome. Input positive a real number for a, b, and c. Numbers must range between 1.0 -10.0");
userInput = JOptionPane.showInputDialog("Input a real number for a.");
a = Double.parseDouble(userInput);// convert String userInput to real
// numbers.
System.out.println("Number for a = " + userInput); // print a
userInput = JOptionPane.showInputDialog("Input a real number for b. ");
b = Double.parseDouble(userInput);
System.out.println("Number for b = " + b); // print b
userInput = JOptionPane.showInputDialog("Input a real number for c.");
c = Double.parseDouble(userInput);
System.out.println("Number for c = " + c); // print c
// calculate quadratic equation 5 times, store in xValues then, print to
// screen.
double product;
double[] xValues = new double[5]; // array index of 5.
for (int i = 4; i >= 0; i--) {
xValues[i] = i + 1; // fills array with numbers 1-5.
// raise x to the i'th degree.
product = a * Math.pow(xValues[i], 2) + b * xValues[i] + c;
// System.out.println("[" + i + "]"+ " " + xValues[i] + " " +// product);
System.out.printf("%d , i " + "%1.2f ", xValues[i] + " " + "%1.3f ", product);
} // end loop
}
}我想这句话会引起问题:
System.out.printf("%d , i " + "%1.2f ", xValues[i] + " " + "%1.3f ", product);
可简化为:
System.out.printf("%d , i %1.2f ", xValues[i] + " %1.3f ", product);
在这里,您可以清楚地看到您试图用字符串xValues[i]+%1.3f“
替换%d
我想你想要的是一些不同的东西。看看API,它应该如下所示
System.out.printf("%d , %1.2f , %1.3f ", i, xValues[i], product);
我试过你的建议,但我有一个错误“线程中的异常”main“java.util.UnknownFormatConversionException:Conversion='1'。@Raven你在格式化syntex时出错了,请看这篇文章,如果有任何问题,请告诉我们