1 选题
|
选题一 |
算术运算测试 |
|
题目要求 |
|
|
|
|
使用Java知识 |
|
|
|
|
选题二 |
简易计算器模拟程序 |
|
题目要求 |
|
|
1.编写一具有GUI的计算器 2.能实现整数的加、减乘除四则运算 |
|
|
使用Java知识 |
|
|
|
2 系统需求分析
2.1 系统可行性分析
一、算术运算测试
1.经济可行性
本次程序使用的Eclipse软件是免费的,此软件能满足现有的需求,不需要经济支持,在经济上可行。
2.技术可行性
运用Eclipse软件,采用Java语言、Java面向对象,技术上可行。
3.操作可行性
对Eclipse软件进行环境变量配置,配置步骤可查、可自行学习,操作可行。
4.法律可行性
法律许可范围
二、简易计算机模拟程序
1.经济可行性
本次程序使用Eclipse软件和Windowbuilder插件,能满足现有需求,不需要经济支持,经济上可行。
2.技术可行性
运用Eclipse软件、Windowbuilder插件、Java语言、Java面向对象,技术上可行。
3.操作可行性
安装Windowbuilder插件,操作简单。
4.法律可行性
法律许可范围
2.2 系统需求分析
2.2.1算术运算测试
首先要用到Scanner、Random包,供用户输入和产生随机数。创建内循环体(for循环),循环体内设置三个随机数,两个用于计算(100以内),一个用于进行判断选择运算符(2以内)。选择运算符后对随机数(100以内)进行运算,跟用户输入的数字进行比较,比较结果进行相应输出并计算分数,内循环内给一个键盘输入口(用户输入yes/no),询问是否进行游戏。外循环(do-while)进行条件判断用户是否继续进行游戏。
2.2.2简易计算器模拟程序
定义新的窗口,在窗口上添加面板,对面板进行布局,面板上添加按钮和标签,对按钮进行监听进行不同的运算方法。
2.3 进度安排
阶段一进度安排如表3-1所示。
表3-1 进度安排表
|
阶段 |
持续时间 |
阶段描述 |
输出 |
|
构思阶段 |
2小时 |
需求分析 |
需求说明,功能模块图 |
|
设计阶段 |
2小时 |
系统设计 |
设计说明-可以画流程图;数据库设计 |
|
实现阶段 |
8小时 |
编写代码 |
项目工程源代码 |
|
2小时 |
系统测试 |
进行黑盒测试(功能测试)-测试说明 |
|
|
运行阶段 |
1小时 |
部署、运行 |
系统使用说明、运维报告、录制视频 |
3 系统设计
3.1 系统设计
3.1.1算术运算
1.for循环
(1)产生十道随机题
(2)里边选择运算符对随机数进行求和、求商,输出问题
(3)给用户键盘输入口,对用户输入的答案进行判断进行相应的输出
(4)提示用户输入(yes/no)提问是否继续游戏
2.do-while循环
(1)执行for循环体
(2)对用户输入的字符进行判断,判断是否继续执行程序
3.1.2简易计算器模拟
1.定义新窗口(Frame)
2.窗口添加面板设置布局(Panel,BorderLayout,GridLayout)
3.面板上添加按钮(Button)
4.添加监听器对按钮进行监听(ActionListener)
4系统实现
一、算术运算测试
1.十道题随机出现
for(int i = 0;i < 10;i++) {
int a = rd.nextInt(100);//获取100以内的数
int b = rd.nextInt(100);
int c = rd.nextInt(2);//获取0和1
int num = 0;
if(c==0) {//对c进行判断然后选择运算符
num = a+b;
System.out.println(a+"+"+b+"=?");
}else{
num = a-b;
System.out.println(a+"-"+b+"=?");
}
图4-1 十道题随机出现
2.用户输入答案与程序运算答案进行比较并统计得分
int x = sc.nextInt();
if(x==num) {
j+=10; //对游戏分数进行统计
System.out.println("回答正确!");
}else System.out.println("回答错误!");
}
System.out.println("最终得分:"+j);
图4-2 答案比较统计分数
3.手动结束
String q = "yes";
do{(省略)
System.out.println("请问是否继续进行游戏(yes/no):");
s = sc.next();
}while(s.equals(q));//判断字符是否相同
System.out.println("游戏结束!");
图4-3 继续进行/结束游戏
二、简易计算器模拟
1.新建窗口设置窗口功能
public class Reg{
public static void main(String[] args){
EventQueue.invokeLater(new Runnable() {//把事件添加到awt的事件处理线程当中
public void run(){
RegFrame frame = new RegFrame();//创建新的窗口
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口
frame.setVisible(true);//窗口可视化
}
});
}
}
class RegFrame extends JFrame{
public RegFrame() {//关于窗口的构造
setTitle("Calculator");//窗口标题
RegPanel panel=new RegPanel();//创建新的面板
add(panel);//往RegFrame窗口中加入面板
pack();//可随意调动窗口
}
}
2.往窗口添加面板设置面板布局
public RegPanel(){
setLayout(new BorderLayout());//东西南北中布局
result=0;
lastCommand="=";
start=true;
display=new JButton("0");//添加一个新按钮
display.setEnabled(false);//display不可用
add(display,BorderLayout.NORTH);//将display放在面板上方
(省略)
add(panel, BorderLayout.CENTER);}
3.面板里边添加按钮,对按钮监听
ActionListener insert=new InsertAction();//注册监听器
ActionListener command=new CommandAction();
panel=new JPanel();
panel.setLayout(new GridLayout(4,4));//表格布局四行四列
addButton("7", insert);//添加组件(按钮)
addButton("8", insert);
addButton("9", insert);
addButton("/", command);
addButton("4", insert);
addButton("5", insert);
addButton("6", insert);
addButton("*", command);
addButton("1", insert);
addButton("2", insert);
addButton("3", insert);
addButton("-", command);
addButton("0", insert);
addButton(".", insert);
addButton("=", command);
addButton("+", command);
4.按钮操作字符插入显示文本
private class InsertAction implements ActionListener{
public void actionPerformed(ActionEvent event){
String input=event.getActionCommand();
if(start){
display.setText("");
start=false;
}
display.setText(display.getText()+input);
}
}
5.执行按钮操作字符的命令
private class CommandAction implements ActionListener{
public void actionPerformed(ActionEvent event){
String command=event.getActionCommand();
if(start){
if (command.equals("-")){
display.setText(command);
start = false;
}
else lastCommand = command;
}else {
calculate(Double.parseDouble(display.getText()));
lastCommand=command;
start=true;
}
}
}
6.点击按钮进行不同运算方法
public void calculate(double x){
if (lastCommand.equals("+")) result += x;
else if (lastCommand.equals("-")) result -= x;
else if (lastCommand.equals("*")) result *= x;
else if (lastCommand.equals("/")) result /= x;
else if (lastCommand.equals("=")) result = x;
display.setText("" + result);
}
图5-1 简易计算器模拟
5 系统测试
表6-1算术运算测试表
|
编号 |
测试功能 |
输入描述 |
预期结果 |
运行结果 |
|
HP01 |
十道题随机 |
1.两运算数随机出现 2.运算符随机出现 |
可以出现两个随机数一个随机运算符 |
正常,与预期结果一致 |
|
HP02 |
用户输入答案对比 计算分数 |
1.用户输入一个答案 2.与程序运算的答案对比 3.进行对应的计算分数 |
用户输入答案与程序计算符合加分,反之不进行加分操作 |
正常,与预期结果一致 |
|
HP03 |
手动控制游戏 |
输入(yes/no)字符判断
|
当输入yes时游戏继续进行,输入no游戏结束 |
正常,与预期结果一致 |
算术运算模板测试主要是针对程序的检测,从测试结果中可以看出该模块的所有功能均能正常实现,且测试结果与预期结果一致。
6 结论和心得
心得:
进行课程设计确实有些许的压力,要积极的把压力化为动力。一些不懂得知识一般都可以在网络上采集到,这使我们的学习更方便,这次设计课程不懂得知识点有很多,有很多自己的盲区,也找到了自己的不足,我会不断补短争取更上一层楼。
浙公网安备 33010602011771号