据我所知,我做得对(显然不是这样) 我正在尝试将字符串更改为双打,因为我无法从JPane获得双倍。它给了我一个没有初始化错误的对象。我该如何解决?
import javax.swing.JOptionPane;
public class jwindows {
public static void main (String args[]) {
double a, b, c;
double sum = a + b + c;
double product = a * b * c ;
double avarge = a * b * c / 3;
String stringA = JOptionPane.showInputDialog
(null, "Please enter first number");
a = Double.parseDouble(stringA);
String stringB = JOptionPane.showInputDialog
(null, "Please enter second number: ");
b = Double.parseDouble(stringB);
String stringC = JOptionPane.showInputDialog
(null, "Please enter third number: ");
c = Double.parseDouble(stringC);
JOptionPane.showInternalMessageDialog
(null, "The sum of the 3 numbers is " + sum);
JOptionPane.showInternalMessageDialog
(null, "The avarge of the 3 numbers is " + avarge);
JOptionPane.showInternalMessageDialog
(null, "The sum of the 3 numbers is " + product);
}
}
答案 0 :(得分:1)
double a, b, c;
double sum = a + b + c;
double product = a * b * c ;
double avarge = a * b * c / 3;
您刚刚定义了变量,但没有初始化它们。在获得a,b,c的所有值后,将它们向右移动。
还有一件事:将showInternalMessageDialog
更改为showMessageDialog
,因为您根本没有父组件。
答案 1 :(得分:0)
变量a
,b
,c
尚未初始化(且不包含任何值),因此sum
,product
和{{ 1}}无法计算。要解决此问题,请在解析avarage
,sum
,product
之后移动avarge
,a
和b
。像这样:
c
答案 2 :(得分:0)
你不会得到变量 sum,averge和product的预期值。你在开头计算它的值:
double a, b, c;
double sum = a + b + c;
double product = a * b * c ;
double avarge = a * b * c / 3;
你必须在这里得到编译错误,因为a,b和c是在使用之前未初始化的局部变量。因此编译器会在这种情况下抛出错误。 即使将其初始化为某个值,也必须在从showInputDialog为这些变量赋值后计算sum,averge和prodcut的值。
尝试使用:
sum = a+b+c;
JOptionPane.showInternalMessageDialog
(null, "The sum of the 3 numbers is " + sum);
averge = (a+b+c)/3;
JOptionPane.showInternalMessageDialog
(null, "The avarge of the 3 numbers is " + avarge);
product = a*b*c;
JOptionPane.showInternalMessageDialog
(null, "The sum of the 3 numbers is " + product);