我正在为学校项目制作一个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));
}
答案 0 :(得分:1)
它确实只计算了2个最后的数字,因为你不存储任何以前的数字。为清楚起见,让我们举例2 + 3 + 4。您的步骤是:
firstnumber
firstnumber
(覆盖我们之前的值)secondnumber
,计算firstnumber
+ secondnumber
使用您的方法应该做的是计算&#39; subresult&#39;在步骤4中(如果需要,在后续步骤中)并将其存储在firstnumber
变量中。