第三次作业 计算器
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
结对伙伴:刘云杰
(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
结对伙伴:刘云杰
相关文章推荐
- IOS圆形头像的设置
- c语言中的变量声明
- 归并排序
- 周日
- sharepoint 2016 学习系列篇(13)-自定义列表应用篇-(2)添加字段栏
- Ruby学习-Ruby类的使用
- ARM应用程序调试
- java实现压缩、解压缩的常见问题Memo
- 计算机C语言的介绍
- PIC单片机DS18B20温度报警程序及仿真
- MongoProperties
- [Codechef October Challenge 2014]刷漆
- 在MyEclipse中导入dom4j包
- 使用uiautomator做UI测试
- sharepoint 2016 学习系列篇(12)-自定义列表应用篇-(1)创建一个自定义列表(Create a custom list)
- 使用NDK编译x264库
- HVM的事件模型
- MongoBean
- String类与date类的转换,date类的计算以及获取当前时间
- UIScrollView实现图片轮播器及其无限循环效果