gpt4 book ai didi

java - 我如何让我的程序从一个输入字段计算两个方程式?

转载 作者:行者123 更新时间:2023-11-29 05:11:18 25 4
gpt4 key购买 nike

我需要从用户那里计算出两个函数,并且想知道完成后如何使用相同的输入字段在对话框中显示两个答案。我在 comp sci 类(class)的第四周,我觉得自己陷入了一些麻烦。我们被要求使用 JOptionPane 来执行此程序,但我想做更多并创建了我自己的程序。

到目前为止我所做的是,该程序已在 JGrasp 和 Eclipse 中正确编译,但在我运行该应用程序时显示了这一点。

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at Nicolas_Carabajal_Assignment3$CalcButtonListener.actionPerformed(Nicolas_Carabajal_Assignment3.java:57)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3322)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:751)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:702)
at java.awt.EventQueue$3.run(EventQueue.java:696)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:724)
at java.awt.EventQueue$4.run(EventQueue.java:722)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:721)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

代码:

 /**
*
*@author ngc5043
*@version 1.0
*/

import javax.swing.*;
import java.awt.event.*;

public class Nicolas_Carabajal_Assignment3 extends JFrame
{
private JPanel panel;
private JLabel messageLabel;
private JTextField ExTextField;
private JButton calcButton;
private final int WINDOW_WIDTH = 310;
private final int WINDOW_HEIGHT = 100;


public static void main(String[] args)
{
Nicolas_Carabajal_Assignment3 main = new Nicolas_Carabajal_Assignment3();
main.buildPanel();
}
public Nicolas_Carabajal_Assignment3()
{
setTitle("Expressions Window");
setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buildPanel();
add(panel);
setVisible(true);
}

private void buildPanel()
{
messageLabel = new JLabel("Please Enter a Number");
ExTextField = new JTextField(10);
calcButton = new JButton("Calculate");
calcButton.addActionListener(new CalcButtonListener());
panel = new JPanel();
panel.add(messageLabel);
panel.add(ExTextField);
panel.add(calcButton);
}

private class CalcButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String input;
int num_ent = 0;
double answerOne;


input = ExTextField.getText();
answerOne = Double.parseDouble(input);
answerOne = num_ent * num_ent;
JOptionPane.showMessageDialog(null, "Your Answer Is" + answerOne);

/**
* here i would like to add another function. input*(n+1.0)/2.0
*Getting the same number from the user and calculating this aswell.
*/



}

}


}

最佳答案

您两次调用了 buildpanel 方法,这就是字符串为空的原因,现在它将字符串转换为 double 并打印出来:

     /**
*
*@author ngc5043
*@version 1.0
*/

import javax.swing.*;
import java.awt.event.*;

public class Nicolas_Carabajal_Assignment3 extends JFrame
{
private JPanel panel;
private JLabel messageLabel;
private JTextField ExTextField;
private JButton calcButton;
private final int WINDOW_WIDTH = 310;
private final int WINDOW_HEIGHT = 100;


public static void main(String[] args)
{
Nicolas_Carabajal_Assignment3 main = new Nicolas_Carabajal_Assignment3();

}
public Nicolas_Carabajal_Assignment3()
{
buildPanel();
setTitle("Expressions Window");
setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

add(panel);
setVisible(true);
}

private void buildPanel()
{
messageLabel = new JLabel("Please Enter a Number");
ExTextField = new JTextField(10);
calcButton = new JButton("Calculate");

panel = new JPanel();
panel.add(messageLabel);
panel.add(ExTextField);
panel.add(calcButton);
calcButton.addActionListener(new CalcButtonListener());
}

private class CalcButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String input;
int num_ent = 0;


input = ExTextField.getText();

System.out.println(input);

double answerOne = Double.parseDouble(input);
System.out.println(answerOne);
answerOne = num_ent * num_ent;
JOptionPane.showMessageDialog(null, "Your Answer Is" + answerOne);

/**
* here i would like to add another function. input*(n+1.0)/2.0
*Getting the same number from the user and calculating this aswell.
*/



}

}


}

关于java - 我如何让我的程序从一个输入字段计算两个方程式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28331780/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com