我在将变量从JTextField存储到变量中以执行二次公式时遇到麻烦。
public void actionPerformed(ActionEvent event) {
int a, b, c; //ax^2 + bx + c
//retrieving whatever is in textfield
String text1 = a1.getText(); //HERE IS WHERE I AM HAVING TROUBLE
String text2 = b2.getText();
String text3 = c3.getText();
//retrieving what is in tf and storing it
a = Integer.parseInt(text1);
b = Integer.parseInt(text2); //TO PLACE INTO HERE
c = Integer.parseInt(text3);
// compute the discriminate
double discriminate = Math.pow(b, 2) - (4 * a * c);
//compute roots
double root1 = ((-1 * b) + discriminate) / (2 * a); //TO USE INTO HERE
double root2 = ((-1 * b) - discriminate) / (2 * a);
result.setText(Double.toString(root1 + root2));
}
最佳答案
您应该输出两个根,但最后一行是将两个根相加并显示总和:
例如,如果您计算,root1 == 1
和root2 == 3
的两个根,则您的行
result.setText(Double.toString(root1 + root2));
将显示
4
您真正想要的是1,3
之类的内容。下一行将执行此操作result.setText(root1 + ","+ root2);;