gpt4 book ai didi

java - 在求解 X 时从 JTextArea 检索 double 值

转载 作者:行者123 更新时间:2023-12-02 11:12:11 26 4
gpt4 key购买 nike

好吧,我对java有点陌生。我正在编写一个程序来求解一步方程。但我在运行它时遇到了一些困难。这是我的主文件 Main.java 的代码:

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

public class Main extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
Solve solve = new Solve();
JButton add = new JButton("Add");
JButton sub = new JButton("Subtract");
JButton mult = new JButton("Multiply");
JButton div = new JButton("Divide");
JButton solv = new JButton("Solve!");
JTextArea one = new JTextArea();
JLabel two = new JLabel(" = ");
JLabel three = new JLabel("X");
JLabel four = new JLabel();
JTextArea five = new JTextArea();
JLabel solved = new JLabel();
JPanel row1 = new JPanel();
JPanel row2 = new JPanel();
JPanel row3 = new JPanel();

public double funct;

public Main() {
super("Solving a one step equation!");
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
GridLayout layout = new GridLayout();
setLayout(layout);

FlowLayout layout1 = new FlowLayout(FlowLayout.CENTER);

row1.setLayout(layout1);
row1.add(add);
row1.add(sub);
row1.add(mult);
row1.add(div);
row1.add(solv);
add(row1);
add.addActionListener(this);
sub.addActionListener(this);
mult.addActionListener(this);
div.addActionListener(this);
solv.addActionListener(this);

GridLayout layout2 = new GridLayout(1, 1, 1, 1);
row2.setLayout(layout2);
row2.add(one, BorderLayout.CENTER);
row2.add(two, BorderLayout.CENTER);
row2.add(three, BorderLayout.CENTER);
row2.add(four, BorderLayout.CENTER);
row2.add(five);
add(row2, BorderLayout.CENTER);

GridLayout layout3 = new GridLayout(5, 5, 5, 5);
row3.setLayout(layout3);
row3.add(solved);
add(row3);
}

public static void main(String[] args) {
Main frame = new Main();
}

public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
if(source == add)
{
four.setText(" + ");
funct = 1;
}
else if(source == sub)
{
four.setText(" - ");
funct = 2;
}
else if(source == mult)
{
four.setText(" * ");
funct = 3;
}
else if(source == div)
{
four.setText(" / ");
funct = 4;
}
if(source == solv)
{
if(funct == 1)
{

double Ones = Double.parseDouble(three.getText());
double Twos = Double.parseDouble(three.getText());
solved.setText("X = " + solve.Add(Ones, Twos));
}
else if(funct == 2)
{
double Ones = Double.parseDouble(three.getText());
double Twos = Double.parseDouble(three.getText());
solved.setText("X = " + solve.Sub(Ones, Twos));
}
else if(funct == 3)
{
double Ones = Double.parseDouble(three.getText());
double Twos = Double.parseDouble(three.getText());
solved.setText("X = " + solve.Mult(Ones, Twos));
}
else if(funct == 4)
{
double Ones = Double.parseDouble(three.getText());
double Twos = Double.parseDouble(three.getText());
solved.setText("X = " + solve.Div(Ones, Twos));
}

}
}

}

这是我的另一个文件 Solve.java 的代码

public class Solve {
public double Add(double One, double Two)
{
return One - Two;
}

public double Sub(double One, double Two)
{
return One + Two;
}

public double Mult(double One, double Two)
{
return One / Two;
}

public double Div(double One, double Two)
{
return One * Two;
}
}

一些帮助将不胜感激。有人看到我做错了什么吗?

最佳答案

单击“Solve”按钮后,您会收到 NumberFormatException。这似乎是一个复制/粘贴问题 - 您没有检索到正确的数字。您正在尝试将“X”字符串转换为 double 。最好为变量指定有意义的名称。要修复异常,请尝试此操作,替换:

double Ones = Double.parseDouble(three.getText());
double Twos = Double.parseDouble(three.getText());

与:

double Ones = Double.parseDouble(one.getText());
double Twos = Double.parseDouble(five.getText());

熟悉 Java Code Conventions , Naming Conventions特别是部分。

关于java - 在求解 X 时从 JTextArea 检索 double 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11065830/

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