在求解X的同时从JTextArea检索double [英] Retrieving a double from a JTextArea while solving for X

查看:96
本文介绍了在求解X的同时从JTextArea检索double的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我是Java的新手.我正在编写一个解决一步方程式的程序.我在运行它时遇到了一些困难.这是我的主文件Main.java的代码:

Ok, I'm kinda new to java. I'm making a program that solves for one step equations. I'm having some difficulties running it though. Here is the code for my main file, 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的代码

Here is the code for my other file, 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;
    }
}

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

Some help would be appreciated. Anyone see what I'm doing wrong?

推荐答案

单击解决"按钮后,您将得到一个NumberFormatException.似乎是复制/粘贴问题-您未获取正确的数字.您正在尝试将'X'字符串转换为双精度.最好给变量赋予有意义的名称.要解决该异常,请尝试以下操作,替换为:

You get a NumberFormatException once 'Solve' button is clicked. It seems like a copy/paste issue - you are not retrieving the correct numbers. You are trying to convert 'X' string to double. It is best if you give meaningful names to your variables. To fix the exception, try this, replace :

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 代码约定命名约定部分.

Get familiar with Java Code Conventions, Naming Conventions section in particular.

这篇关于在求解X的同时从JTextArea检索double的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆