gpt4 book ai didi

java - 为我的计算器小程序创建一个类

转载 作者:行者123 更新时间:2023-11-30 07:05:15 25 4
gpt4 key购买 nike

好的,这是我开始使用的计算器的代码。

import java.awt.*;
import java.applet.*;

public class Calculator extends Applet
{

private Label calculatorL; //Sets the label
private boolean firstDigit = true; //Sets Boolean firstDigit to be true
private float savedValue = 0.0f;//Sets saved value to 0
private String operator = "="; //Sets Default operator

public void init ()
{
setLayout (new BorderLayout()); //Sets the layout
add ("North", calculatorL = new Label ("0", Label.RIGHT));
Panel calculatorPanel = new Panel(); //Adding a panel for the buttons
calculatorPanel.setLayout (new GridLayout (4, 4)); //Adding a 4 * 4 grid for the layout of the buttons
calculatorPanel.setBackground(Color.CYAN);
calculatorPanel.setForeground(Color.BLUE);
addButtons (calculatorPanel, "123-");
addButtons (calculatorPanel, "456*");
addButtons (calculatorPanel, "789/");
addButtons (calculatorPanel, ".0=+");
add("Center", calculatorPanel);
}

public boolean action (Event event, Object inputobject)
{
if (event.target instanceof Button)
{
String inputstring = (String)inputobject;
if ("0123456789.".indexOf (inputstring) != -1)
{
if (firstDigit)
{
firstDigit = false;
calculatorL.setText (inputstring);
}
else
{
calculatorL.setText(calculatorL.getText() + inputstring);
}
}
else
{
if(!firstDigit)
{
solve(calculatorL.getText());
firstDigit = true;
}
operator = inputstring;
}
return true;
}
return false;
}

public void solve (String valueToBeComputed)
{
float sValue = new Float (valueToBeComputed).floatValue();
char c = operator.charAt (0);
switch (c)
{
case '=': savedValue = sValue;
break;
case '+': savedValue += sValue;
break;
case '-': savedValue -= sValue;
break;
case '*': savedValue *= sValue;
break;
case '/': savedValue /= sValue;
break;
}
calculatorL.setText (Float.toString(savedValue));
}

public void addButtons (Panel panel, String labels)
{
int count = labels.length();
for (int i = 0; i<count; i ++)
{
panel.add (new Button (labels.substring(i,i+1)));
}
}
}

我想做的是将我的求解器代码转移到它自己的单独的类中。

这就是我目前所拥有的

import java.awt.*;

public class Solver
{

private String operator = "="; //Sets Default operator
private float savedValue = 0.0f;//Sets saved value to 0

public void solve (String valueToBeComputed)
{
float sValue = new Float (valueToBeComputed).floatValue();
char c = operator.charAt (0);
switch (c)
{
case '=': savedValue = sValue;
break;
case '+': savedValue += sValue;
break;
case '-': savedValue -= sValue;
break;
case '*': savedValue *= sValue;
break;
case '/': savedValue /= sValue;
break;
}
calculatorL.setText (Float.toString(savedValue));
}
}

对于使用/创建我自己的自定义类确实很陌生。可以使用一些帮助来传输和设置求解器类。

最佳答案

移动代码有时称为重构...

您可以在新类中将 solve 方法设置为静态,然后像这样调用它

Solver.solve(calculatorL.getText());

或者使用

实例化一个 Solve 对象
Solve mySolve = new Solve();

然后调用该对象的方法:

mySolve.solve(calculatorL.getText());

最后,要返回计算值,您应该将方法类型从 void 更改为 Stringfloat,然后将其传递给 calculatorL.setText().

关于java - 为我的计算器小程序创建一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40256899/

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