计算器项目问题
好吧,我在使用这个计算器时遇到了问题。我被分配了这个项目,我只会使用加法和减法。作业的第一部分是使用 Scanner 类创建它。所以我创建了一个循环,计算器可以工作......但是乘法和除法关闭了。它基本上只是不断地添加数字...就像 3*6 的答案将是 24...但是然后我做 2 * 1 它将是类似 26 之类的东西。哦,是的,我将类保存为不同的文件.. ..有人可以帮我解决这个问题
吗?
public class Calculator_part2 extends calculator_gui{
public static void main(String[]args)
{
calculator_gui n = new calculator_gui();
n.gui();
}
}
import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class calculator_gui implements ActionListener
{
JFrame frame = new JFrame(" Calculator " );
JPanel panel = new JPanel(new FlowLayout());
JTextArea text = new JTextArea(1,20);
JButton but1 = new JButton("1");
JButton but2 = new JButton("2");
JButton but3 = new JButton("3");
JButton but4 = new JButton("4");
JButton but5 = new JButton("5");
JButton but6 = new JButton("6");
JButton but7 = new JButton("7");
JButton but8 = new JButton("8");
JButton but9 = new JButton("9");
JButton but0 = new JButton("0");
JButton add = new JButton( " + ");
JButton sub = new JButton( " - ");
JButton multi = new JButton (" * ");
JButton div = new JButton("/");
JButton buteq = new JButton (" = ");
JButton butclear = new JButton("C");
Double number1,number2,result = 0.0,temp = 0.0;
int addc = 0, subc=0,multic=0,divc=0,i = 0;
public void gui()
{
frame.setVisible(true);
frame.setSize(250,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
panel.add(text);
panel.add(but1);
panel.add(but2);
panel.add(but3);
panel.add(but4);
panel.add(but5);
panel.add(but6);
panel.add(but7);
panel.add(but8);
panel.add(but9);
panel.add(but0);
panel.add(add);
panel.add(sub);
panel.add(multi);
panel.add(div);
panel.add(buteq);
panel.add(butclear);
but1.addActionListener(this);
but2.addActionListener(this);
but3.addActionListener(this);
but4.addActionListener(this);
but5.addActionListener(this);
but6.addActionListener(this);
but7.addActionListener(this);
but8.addActionListener(this);
but9.addActionListener(this);
but0.addActionListener(this);
add.addActionListener(this);
sub.addActionListener(this);
multi.addActionListener(this);
div.addActionListener(this);
buteq.addActionListener(this);
butclear.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if(source == butclear)
{
//number1 = 0.0;
//number2 = 0.0;
text.setText("");
}
if(source == but1)
{
text.append("1");
}
if(source == but2)
{
text.append("2");
}
if(source == but3)
{
text.append("3");
}
if(source == but4)
{
text.append("4");
}
if(source == but5)
{
text.append("5");
}
if(source == but6)
{
text.append("6");
}
if(source == but7)
{
text.append("7");
}
if(source == but8)
{
text.append("8");
}
if(source == but9)
{
text.append("9");
}
if(source == but0)
{
text.append("0");
}
if(source == add)
{
number1 = number_reader();
text.setText("");
addc=1;
subc=0;
multic=0;
divc=0;
}
if(source == sub)
{
number1 = number_reader();
text.setText("");
addc=0;
subc=1;
multic=0;
divc=0;
}
if(source == multi)
{
number1 = number_reader();
text.setText("");
addc=0;
subc=0;
multic=1;
divc=0;
}
if(source == div)
{
number1 = number_reader();
text.setText("");
addc=0;
subc=0;
multic=0;
divc=1;
}
if(source == buteq)
{
number2 = number_reader();
if(addc>0)
{
result = number1 + number2;
text.setText(Double.toString(result));
}
if(subc>0)
{
result = number1 - number2;
text.setText(Double.toString(result));
}
if(multic>0)
{
for( double i = 0;i <number2;i++)
{
result += number1;
}
text.setText(Double.toString(result));
}
if(divc>0)
{
for (i = 0; temp < number1; i++)
{
temp += number2;
}
text.setText(Double.toString(i));
}
}
}
public double number_reader()
{
double num1;
String s;
s = text.getText();
num1 = Double.valueOf(s);
return num1;
}
}
Ok I'm having a problem with this calculator. I've been assigned the project and I can only use addition and subtraction. The first part of the assignment was to create it using Scanner class. So I created a loop and the calculator works...but the multiplication and divison is off. It basically just keeps adding numbers...like for 3*6 the answer will be 24...but then I do 2 * 1 it'll be something like 26 or something.Oh yeah I saved the classes as different files....can somebody help me with this
here it is
public class Calculator_part2 extends calculator_gui{
public static void main(String[]args)
{
calculator_gui n = new calculator_gui();
n.gui();
}
}
import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class calculator_gui implements ActionListener
{
JFrame frame = new JFrame(" Calculator " );
JPanel panel = new JPanel(new FlowLayout());
JTextArea text = new JTextArea(1,20);
JButton but1 = new JButton("1");
JButton but2 = new JButton("2");
JButton but3 = new JButton("3");
JButton but4 = new JButton("4");
JButton but5 = new JButton("5");
JButton but6 = new JButton("6");
JButton but7 = new JButton("7");
JButton but8 = new JButton("8");
JButton but9 = new JButton("9");
JButton but0 = new JButton("0");
JButton add = new JButton( " + ");
JButton sub = new JButton( " - ");
JButton multi = new JButton (" * ");
JButton div = new JButton("/");
JButton buteq = new JButton (" = ");
JButton butclear = new JButton("C");
Double number1,number2,result = 0.0,temp = 0.0;
int addc = 0, subc=0,multic=0,divc=0,i = 0;
public void gui()
{
frame.setVisible(true);
frame.setSize(250,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
panel.add(text);
panel.add(but1);
panel.add(but2);
panel.add(but3);
panel.add(but4);
panel.add(but5);
panel.add(but6);
panel.add(but7);
panel.add(but8);
panel.add(but9);
panel.add(but0);
panel.add(add);
panel.add(sub);
panel.add(multi);
panel.add(div);
panel.add(buteq);
panel.add(butclear);
but1.addActionListener(this);
but2.addActionListener(this);
but3.addActionListener(this);
but4.addActionListener(this);
but5.addActionListener(this);
but6.addActionListener(this);
but7.addActionListener(this);
but8.addActionListener(this);
but9.addActionListener(this);
but0.addActionListener(this);
add.addActionListener(this);
sub.addActionListener(this);
multi.addActionListener(this);
div.addActionListener(this);
buteq.addActionListener(this);
butclear.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if(source == butclear)
{
//number1 = 0.0;
//number2 = 0.0;
text.setText("");
}
if(source == but1)
{
text.append("1");
}
if(source == but2)
{
text.append("2");
}
if(source == but3)
{
text.append("3");
}
if(source == but4)
{
text.append("4");
}
if(source == but5)
{
text.append("5");
}
if(source == but6)
{
text.append("6");
}
if(source == but7)
{
text.append("7");
}
if(source == but8)
{
text.append("8");
}
if(source == but9)
{
text.append("9");
}
if(source == but0)
{
text.append("0");
}
if(source == add)
{
number1 = number_reader();
text.setText("");
addc=1;
subc=0;
multic=0;
divc=0;
}
if(source == sub)
{
number1 = number_reader();
text.setText("");
addc=0;
subc=1;
multic=0;
divc=0;
}
if(source == multi)
{
number1 = number_reader();
text.setText("");
addc=0;
subc=0;
multic=1;
divc=0;
}
if(source == div)
{
number1 = number_reader();
text.setText("");
addc=0;
subc=0;
multic=0;
divc=1;
}
if(source == buteq)
{
number2 = number_reader();
if(addc>0)
{
result = number1 + number2;
text.setText(Double.toString(result));
}
if(subc>0)
{
result = number1 - number2;
text.setText(Double.toString(result));
}
if(multic>0)
{
for( double i = 0;i <number2;i++)
{
result += number1;
}
text.setText(Double.toString(result));
}
if(divc>0)
{
for (i = 0; temp < number1; i++)
{
temp += number2;
}
text.setText(Double.toString(i));
}
}
}
public double number_reader()
{
double num1;
String s;
s = text.getText();
num1 = Double.valueOf(s);
return num1;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在
performAction
底部将结果重置为0.0。此外,您的除法实现将i
设置为文本。这绝对是不对的。You need to reset result to 0.0 at the bottom of
performAction
. Furthermore your implementation of division setsi
as the text. That is definitely not right.我相信这是因为你的每个 if 语句都会执行,因为你永远不会将值恢复为 0,因此每个 if 语句都会继续执行。
I believe its because each of your if statements will execute since you never restore the values back to 0 therefore each if statement continues to execute down the road.