我使用Java swing库构建计算器。除了actionEvent循环中的乘法和除法运算符外,其他所有工作都是有效的。所有其他操作员完全工作。Java计算器运算符错误
这是发生错误: 我曾尝试在代码
计算器的这一部分try语句:
计算器乘法错误:
首先你输入号码
然后按该假设以清除文本框操作员 - 错误发生在此步骤
然后就进入第二数目
然后按=按钮输出答案
图片错误的:
if(e.equals("*"))
{
fnum = txt.getText();
logic.setTotal(fnum);
op = "*";
txt.setText(""); // error occurs here, textfield isn't cleared
JOptionPane.showMessageDialog(null, fnum); //messagebox to see if fnum contains the string from the textfield
}
if(e.equals("/"))
{
fnum = txt.getText();
op = "/";
txt.setText("");
}
动作事件循环/功能:
public void actionPerformed(ActionEvent ea)
{
else if(op.equals("*"))
{
logic.setTotal(fnum);
logic.multiplication(snum);
total1 = logic.total;
}
else if(op.equals("/"))
{
logic.setTotal(fnum);
logic.divide(snum);
total1 = logic.total;
}
txt.setText(""+total1);
}
逻辑是内部类
内部类:
public class Inner extends Calculators{
public double total;
public Inner()
{
total = 0;
}
public void setTotal(String n)
{
total = convertToNumber(n);
}
public void divide(String n)
{
total /= convertToNumber(n);
}
public void multiplication(String n)
{
total *=convertToNumber(n);
}
}
如果你感到困惑,请索要更多的代码,因为我不能包含所有的代码。
Code if you want to try it out yourself
这让我很担心:公共类Inner扩展计算器{'。为什么内在扩展计算器?这表明滥用继承权。否则,如果没有[最小示例程序](http://stackoverflow.com/help/mcve),就很难回答你的问题。 –
内部类使用Calculator方法和变量,因为它是Calculator的“子类”。我无法添加所有的代码,因为它非常广泛。 –
你看到什么特别的错误?发生错误时该类的代码将会很有帮助。 – MaxZoom