您的位置:首页 > 其它

第三次作业 计算器

2015-10-18 21:39 246 查看
需求分析:用户选择计算类型,然后由软件随机生成数据,然后用户给出运算结果,运算软件要判断是否正确,并在软件结束时显示统计结果。

(1):做一个计算器窗口,添加上所有控件和运算符,便于用户输入和计算;

public static void main(String[] args)
{
Frame frame = new Frame("计算器");
frame.setSize(300, 200);
frame.setLocation(300, 200);
frame.addWindowListener(new Listener1());
final TextField tf1 = new TextField(8);
final Choice choice = new Choice();
choice.addItem("+");
choice.addItem("-");
choice.addItem("*");
choice.addItem("/");
final TextField tf2 = new TextField(8);
Label label = new Label("=");
final TextField tf3 = new TextField(8);
Button button = new Button("计算");
frame.add(tf1);
frame.add(choice);
frame.add(tf2);
frame.add(label);
frame.add(tf3);
frame.add(button);
frame.setLayout(new FlowLayout());
button.addActionListener(new ActionListener()

(2):写出运算公式

public void actionPerformed(ActionEvent arg0)

{

String s1 = tf1.getText();

String s2 = tf2.getText();

String ch = choice.getSelectedItem();

double d1 = Double.parseDouble(s1);

double d2 = Double.parseDouble(s2);

double d = 0; if (ch.equals("+"))

{

d = d1 + d2;

} else if (ch.equals("-"))

{

d = d1 - d2;

} else if (ch.equals("*"))

{

d = d1 * d2;

} else {

d = d1 / d2;

}

tf3.setText(d + "");

}

});

frame.setVisible(true);

}

}

(3):最后加入一个计时器,但是没有运行出来;

import java.io.IOException;

public class Timer {

public static void main(String[]args)

{

Timer timer = new Timer();

timer.schedule(new MyTask(),1000,2000);

while (true)

{

try {

int ch = System.in.read();

if (ch-'c'==0)

{

timer.cancel();

}

} catch (IOException e)

{

e.printStackTrace();

}

}

}

private void cancel() {

}

private void schedule(MyTask myTask, int i, int j) {

}

}



总结:结对编程,不同于一个人编程,这里要有两个人的思维,一开始我们都很迷茫,慢慢的分析,有些思路后就彼此交流,如此反复,
最后一起把程序作出来,感觉好吃力啊,毕竟能力有限,只能做到如此而已。。。本次作业用了两个上午,但是还是没有好的结果,心有

余而力不足,肚子里的墨水实在是太少了。

合作图片:



学号:1010

姓名:郭冰倩

博客名:guobingqian

博客链接:http://www.cnblogs.com/gbq0205

结对伙伴:刘云杰
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: