gpt4 book ai didi

java - 简单逻辑的计算器问题

转载 作者:行者123 更新时间:2023-11-30 04:17:51 24 4
gpt4 key购买 nike

我是 Java 新手,我给自己设定的任务是尝试创建一个简单的计算器(和 GUI),以提高我对该语言的理解和技能。

获取此代码:

import java.awt.FlowLayout;
import java.io.*;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class calc extends JFrame {


public JTextField input;
public JTextField output;
public JPanel Window;
public JButton math_button[] = new JButton[5];
public JButton number_button[] = new JButton[10];
public String[] number_button_name = {"1","2","3","4","5","6","7","8","9","0"};
public String[] name = {"Add", "Mulitply", "Divide", "Subtract", "Equals"};
public JFrame frame = new JFrame();
public JPanel math_panel = new JPanel();
public JPanel number_panel = new JPanel();
public JTextField math_input = new JTextField();
boolean trrgger = false;

thehandler handler = new thehandler();

public void go()
{

for(int b=0; b<number_button.length; b++)
{
number_button[b] = new JButton(number_button_name[b]);
number_button[b].addActionListener(handler);
number_panel.add(number_button[b]);

}

for(int i=0; i<math_button.length;i++)
{
math_button[i] = new JButton(name[i]);
math_button[i].addActionListener(handler);
math_panel.add(math_button[i]);

}


frame.getContentPane().add(BorderLayout.NORTH, math_input);
frame.getContentPane().add(BorderLayout.SOUTH, math_panel);
frame.getContentPane().add(BorderLayout.CENTER, number_panel);
frame.setSize(400,400);
frame.setVisible(true);



}

//Method to handle the math and return the results of whichever 'button' was pressed
static int Math(String button_num, int first_num, int second_num)
{
int total = 0;
if(button_num == "Add")
{
total = first_num + second_num;
}

else if (button_num == "Mulitply") //multiply
{
total = first_num * second_num;
}

else if (button_num == "Divide") //divide
{
total = first_num / second_num;
}

else if (button_num == "Substract") //subtract
{
total = first_num - second_num;
}

else if (button_num == "Equals") //subtract
{
total = total;
}

return total;
}

//Action Events - Code that is triggered once the associated button is clicked


public class thehandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{


for (int h = 0; h <math_button.length; h++)
{
if(event.getSource()==math_button[h])
{
int firstn = Integer.parseInt(math_input.getText());
math_input.setText("");
int secondn = Integer.parseInt(math_input.getText());

System.out.println(calc.Math(math_button[h].getText(), firstn, secondn));

}

}

for(int n=0; n<number_button.length; n++)
{
if(event.getSource()==number_button[n])
{
String number_clicked = (number_button[n].getText());
String number = math_input.getText();
math_input.setText(number + number_clicked);

}



}

}
}

}

此代码背后的想法是创建一个简单的 GUI,并允许用户输入所需数量的数字,然后按“等于”按钮显示结果。然而,正如我所说,我的逻辑有问题。我可以从 JTextField 获取第一个输入的数字,在第一个变量初始化后清除文本,但这就是程序崩溃的地方。变量“second_num”作为空白传递给“Math”方法(这会引发错误),因为这就是我告诉 ActionEvent 要做的事情,以便更流畅地使用程序,没有用户希望必须继续清除使用计算器时的输入框。

有人有什么想法吗?

谢谢

最佳答案

int firstn = Integer.parseInt(math_input.getText());
math_input.setText("");
int secondn = Integer.parseInt(math_input.getText());

您究竟希望上述几行代码做什么?您将从 math_input 获取文本。然后将其设置为空字符串。通过立即取回字符串,您希望得到除空字符串之外的其他内容?

正确的做法是:

  • 第一次调用处理程序(即单击“数学”按钮)时收集输入。将其存储在某个地方。
  • 下次调用此处理程序时,您将获得下一个输入。依此类推,直到用户单击“=”来计算整个表达式。

建议:如果您是 java 新手,您可能会发现首先在命令行上创建计算器更容易。计算器的功能不需要 GUI。在命令行中收集输入更加简单。如果您成功了,那么您可以继续使用 Swing 等更奇特的东西

关于java - 简单逻辑的计算器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17906925/

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