gpt4 book ai didi

java - 我想在我的 jframe 计算器中进行多次计算,例如 2+2+2

转载 作者:行者123 更新时间:2023-12-01 09:19:21 26 4
gpt4 key购买 nike

我正在为学校项目制作一个 jFrame 计算器,但每当我尝试计算 2+2+2 之类的东西时,我都会得到 4。就像它只计算最后 2 位数字一样。有谁知道如何解决这一问题?这是我的代码

//numbers
private void NulNulActionPerformed(java.awt.event.ActionEvent evt) {
Display.setText(Display.getText()+NulNul.getText());
}

private void NulActionPerformed(java.awt.event.ActionEvent evt) {
Display.setText(Display.getText()+Nul.getText());
}

private void PuntActionPerformed(java.awt.event.ActionEvent evt) {
if(PointClick == 0){
Display.setText(Display.getText()+Punt.getText());
}
else{
Punt.enable(false);

}
PointClick = 1;
}

private void EenActionPerformed(java.awt.event.ActionEvent evt) {
Display.setText(Display.getText()+Een.getText());
}

private void TweeActionPerformed(java.awt.event.ActionEvent evt) {
Display.setText(Display.getText()+Twee.getText());
}

private void DrieActionPerformed(java.awt.event.ActionEvent evt) {
Display.setText(Display.getText()+Drie.getText());
}

private void VierActionPerformed(java.awt.event.ActionEvent evt) {
Display.setText(Display.getText()+Vier.getText());
}

private void VijfActionPerformed(java.awt.event.ActionEvent evt) {
Display.setText(Display.getText()+Vijf.getText());
}

private void ZesActionPerformed(java.awt.event.ActionEvent evt) {
Display.setText(Display.getText()+Zes.getText());
}

private void ZevenActionPerformed(java.awt.event.ActionEvent evt) {
Display.setText(Display.getText()+Zeven.getText());
}

private void AchtActionPerformed(java.awt.event.ActionEvent evt) {
Display.setText(Display.getText()+Acht.getText());
}

private void NegenActionPerformed(java.awt.event.ActionEvent evt) {
Display.setText(Display.getText()+Negen.getText());
}

//Clear
private void ClearActionPerformed(java.awt.event.ActionEvent evt) {
Display.setText("");
PointClick = 0;
}

//Back
private void BackActionPerformed(java.awt.event.ActionEvent evt) {
String text;
text = Display.getText();
int len = text.length();
text = text.substring(0, len-1);
Display.setText(text);
}

private void PlusActionPerformed(java.awt.event.ActionEvent evt) {
firstnumber = Double.parseDouble(Display.getText());
Display.setText("");
PlusClick= 1;
MinusClick = MultiplyClick = DivideClick = PercentClick = PowerClick = PointClick = 0;
}

private void EqualsActionPerformed(java.awt.event.ActionEvent evt) {
secondnumber = Double.parseDouble(Display.getText());
if(PlusClick > 0){
Result = firstnumber + secondnumber;
Display.setText(String.valueOf(Result));
}
else if(MinusClick > 0){
Result = firstnumber - secondnumber;
Display.setText(String.valueOf(Result));
}
else if(MultiplyClick > 0){
Result = firstnumber * secondnumber;
Display.setText(String.valueOf(Result));
}
else if(DivideClick > 0){
Result = firstnumber / secondnumber;
Display.setText(String.valueOf(Result));
}
else if(PowerClick > 0){
for(int i=1;i<=secondnumber;i++)
{
Result = Result * firstnumber;
}
Display.setText(String.valueOf(Result));
}
else if(PercentClick > 0){
Result = (firstnumber * secondnumber) / 100;
Display.setText(String.valueOf(Result));
}
}

private void MinActionPerformed(java.awt.event.ActionEvent evt) {
firstnumber = Double.parseDouble(Display.getText());
Display.setText("");
MinusClick= 1;
PlusClick = MultiplyClick = DivideClick = PercentClick = PowerClick = PointClick = 0;
}

private void KeerActionPerformed(java.awt.event.ActionEvent evt) {
firstnumber = Double.parseDouble(Display.getText());
Display.setText("");
MultiplyClick= 1;
MinusClick = PlusClick = DivideClick = PercentClick = PowerClick = PointClick = 0;
}

private void DelenActionPerformed(java.awt.event.ActionEvent evt) {
firstnumber = Double.parseDouble(Display.getText());
Display.setText("");
DivideClick= 1;
MinusClick = MultiplyClick = PlusClick = PercentClick = PowerClick = PointClick = 0;
}

private void WortelActionPerformed(java.awt.event.ActionEvent evt) {
firstnumber = Double.parseDouble(Display.getText());
Display.setText(String.valueOf(Math.sqrt(firstnumber)));
}

private void PIActionPerformed(java.awt.event.ActionEvent evt) {
Display.setText(String.valueOf(Math.PI));
}

private void MachtActionPerformed(java.awt.event.ActionEvent evt) {
firstnumber = Double.parseDouble(Display.getText());
Display.setText("");
PowerClick= 1;
MinusClick = MultiplyClick = PlusClick = PercentClick = DivideClick = PointClick = 0;
}

private void ProcentActionPerformed(java.awt.event.ActionEvent evt) {
firstnumber = Double.parseDouble(Display.getText());
Display.setText("");
PercentClick= 1;
MinusClick = MultiplyClick = PlusClick = PowerClick = DivideClick = PointClick = 0;
}

private void InverseActionPerformed(java.awt.event.ActionEvent evt) {
firstnumber = Double.parseDouble(Display.getText());
Display.setText(String.valueOf(1 / firstnumber));
}

private void PlusminusActionPerformed(java.awt.event.ActionEvent evt) {
firstnumber = Double.parseDouble(Display.getText());
Display.setText(String.valueOf((-1) * firstnumber));
}

最佳答案

它确实只计算最后 2 位数字,因为您不存储任何前面的数字。为了清楚起见,我们将示例设为 2+3+4。您的步骤是:

  1. 阅读 2
  2. 读取 +,将 2 存储为 firstnumber
  3. 阅读 3
  4. 读取 +,将 3 存储为 firstnumber(覆盖我们之前的值)
  5. 阅读 4
  6. 读取=,将4存储为第二个数字,计算第一个数字 + 第二个数字

使用您的方法,您应该做的是在步骤 4 中计算“子结果”(如果需要,在接下来的步骤中)并将其存储在 firstnumber 变量中。

关于java - 我想在我的 jframe 计算器中进行多次计算,例如 2+2+2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40271375/

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