gpt4 book ai didi

java - 如何从 TextField 获取用户输入并将其转换为 Double?

转载 作者:可可西里 更新时间:2023-11-01 13:27:24 25 4
gpt4 key购买 nike

我正在使用 Eclipse 构建一个计算器,但遇到了麻烦,因为我需要用户输入 2 个值。这是我的运行类代码。

import display.Gui;

public class Main {

public static void main(String argsp[]) {

Gui window = new Gui();
double a = 0, b = 0, c = 0;
String operator;
boolean calculate = true;

window.setVisible(true);
window.setSize(500, 400);
window.setResizable(false);
window.setLocationRelativeTo(null);

while (calculate) {
window.textArea_1.append("Enter an equation.\n");
a = Double.parseDouble(window.textField.getText());
operator = window.textField.getText();
b = Double.parseDouble(window.textField.getText());

if (operator.contains("+"))
c = a + b;

if (operator.contains("-"))
c = a - b;

if (operator.contains("*"))
c = a * b;

if (operator.contains("/"))
c = a / b;

if (operator.contains("x^2"))
c = a * a;

if (operator.contains("sqrt"))
c = Math.sqrt(a);

if (operator.contains("%"))
c = a / 100;

window.textArea.append("" + c + "\n");
window.textArea.append("");
window.textArea.append("Would you like to make another calculation? [Yes/No]\n");

String calculation = window.textField.getText();

try {
if (calculation.equalsIgnoreCase("Yes"))
calculate = true;

if (calculation.equalsIgnoreCase("No"))
calculate = false;
} catch (Exception e) {
window.textArea_1.append("Please enter yes or no");
}

}
}

}

这是我的 JFrame 类:

import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.BorderLayout;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Gui extends JFrame {

public JTextArea textArea, textArea_1;
public JTextField textField;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Gui frame = new Gui();
frame.setVisible(false);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public Gui() {
setBounds(100, 100, 450, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new BorderLayout(0, 0));

textField = new JTextField();
textField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent Ev) {
textArea.append(textField.getText() + "\n");
textField.setText("");
}
});
textField.requestFocus();
getContentPane().add(textField, BorderLayout.SOUTH);
textField.setColumns(10);

textArea = new JTextArea();
textArea.setEditable(false);
textArea.setPreferredSize(new Dimension(215, 200));
getContentPane().add(textArea, BorderLayout.WEST);

textArea_1 = new JTextArea();
textArea_1.setEditable(false);
textArea_1.setPreferredSize(new Dimension(215, 200));
getContentPane().add(textArea_1, BorderLayout.EAST);

}

我尝试使用 Double.parseDouble(window.textField.getText());

但这没有用。我怎样才能让它发挥作用?提前致谢。

最佳答案

首先,我认为您的程序设计存在一些问题。为什么不使用事件(按钮点击、击键等)来触发计算?我没有看到这个程序中 while 循环的好处。

此外,正如一些人已经指出的那样,您的代码甚至在用户输入之前就从文本字段中读取和解析值。这肯定会产生无效的结果。

尝试类似(未测试):

calcButton = new JButton("Calculate");
calcButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent Ev) {
actionCalc();
}
});


public void actionCalc(){
// get the string
// validate string (check for empty string etc)
// parse to Double
Double val = Double.parseDouble(window.textField.getText());
...
}

关于java - 如何从 TextField 获取用户输入并将其转换为 Double?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12451421/

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