一、设计思想
1.功能较多必须有菜单选择项,将一个大程序分为若干个功能模块的小程序,逐个实现
2.针对题目避免重复时先将已生成的算式保存,然后将下一条生成的式子进行判断是否已生成,
如果生成则返回循环语句的上一条,重新生成四则运算式子。
3.针对可定制,可以申请动态存储内存,通过用户输入来完成定制。
4.针对可以控制参数,相对菜单,每个菜单功能对应一个控制参数,每个参数功能对应一个函数通过switch语句判断。
4.1针对是否有乘除法、通过用户选择,来确定,实现是通过定义运算符号定义顺序
如Fuhao={+,-,*,/}产生循环式可产生前两条和前四条运算
其它以此有针对性地进行算法分析实现
二、代码
package com.minirisoft;
import java.util.*;
public class Sizeyunsuan {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.println("请输入要输出多少道运算题");
int h=input.nextInt();
String [] arr1=new String[h];
System.out.println("请输入你要计算数的最大值");
int g=input.nextInt();
System.out.println("**************菜单选择程序****************");
System.out.println("请选择要使用的功能:");
System.out.println("1. 整数加减法运算!");
System.out.println("2. 整数四则运算!");
System.out.println("3. 分数的加减运算");
System.out.println("4. 分数的四则运算");
System.out.println(" *********************************************");
Random random = new Random();
char arr[] = {'+','-','*','/'};
int a=input.nextInt();
switch(a){
case 1:
{
System.out.println("请选择减法是否有负数;0:无负数,1:有负数");
int f=input.nextInt();
int i;
switch(f)
{
case 0:
{
for(i=0;i<h;i++)
{
String str=random.nextInt(g)+" "+arr[random.nextInt(2)]+" "+random.nextInt(g)+"="; System.out.println(str);
}
break;
}
case 1:
{
for(i=0;i<h;i++)
{
System.out.println(-random.nextInt()%g+" "+arr[random.nextInt(2)]+" "+(-random.nextInt())%g+"=");
}
break;
}
default:
System.out.println("输入的数有误,请输入0或1");
}
break;
}
case 2:
{
System.out.println("**************四则运算菜单选择程序****************");
System.out.println("请选择要使用的功能:");
System.out.println("1. 正整数无余数四则运算!");
System.out.println("2. 可负整数无余数四则运算!");
System.out.println("3. 正整数可有余数四则运算");
System.out.println("4. 可负整数可有余数四则运算");
System.out.println(" *********************************************");
int f=input.nextInt();
switch(f)
{
case 1:
{
for(int i=0;i<h;i++)
{
int u=random.nextInt(g);
int v=random.nextInt(g)+1;
char w=arr[random.nextInt(4)];
if(u%v==0 && w=='/'){
System.out.println(u+" "+w+" "+v+"=");
}
else
System.out.println(u+" "+arr[random.nextInt(3)]+" "+v+"=");
}
break;
}
case 2:
{
for(int i=0;i<h;i++)
{
int u=-random.nextInt()%g;
int v=-random.nextInt()%g;
char w=arr[random.nextInt(4)];
if(v!=0 && u%v==0 && w=='/' )
System.out.println(u+" "+w+" "+v+"=");
else
System.out.println(u+" "+arr[random.nextInt(3)]+" "+v+"=");
}
break;
}
case 3:
{
for(int i=0;i<h;i++)
{
int u=random.nextInt(g);
int v=random.nextInt(g)+1;
char w=arr[random.nextInt(4)];
System.out.println(u+" "+w+" "+v+"=");
}
break;
}
case 4:
{
for(int i=0;i<h;i++)
{
int u=-random.nextInt()%g;
int v=-random.nextInt()%g;
char w=arr[random.nextInt(4)];
System.out.println(u+" "+w+" "+v+"=");
}
break;
}
default:
System.out.print("输入的数有误,请输入1--4的整数");
} }
case 3:
{
System.out.println("请选择减法是否有负数;0:无负数,1:有负数");
int f=input.nextInt();
switch(f)
{
case 0:
{
for(int i=0;i<h;i++)
{
System.out.println(random.nextInt(g)+"/"+random.nextInt(g)+1+" "+arr[random.nextInt(2)]+" "+random.nextInt(g)+"/"+random.nextInt(g)+1+"=");
}
break;
}
case 1:
{
for(int i=0;i<h;i++)
{
System.out.println(-random.nextInt()%g+"/"+random.nextInt(g)+1+" "+arr[random.nextInt(2)]+" "+-random.nextInt()%g+"/"+random.nextInt(g)+1+"=");
}
break;
}
default:
System.out.println("输入的数有误,请输入0或1");
}
break;
}
case 4:
{
System.out.println("**************四则运算菜单选择程序****************");
System.out.println("请选择要使用的功能:");
System.out.println("1. 正分数四则运算!");
System.out.println("2. 可负分数四则运算!");
System.out.println(" *********************************************");
int f=input.nextInt();
switch(f)
{
case 1:
{
for(int i=0;i<h;i++)
{
int u=random.nextInt(g);
int v=random.nextInt(g)+1;
int x=random.nextInt(g);
int y=random.nextInt(g)+1;
char w=arr[random.nextInt(4)];
System.out.println(u+"/"+v+" "+w+" "+x+"/"+y+"=");
}
break;
}
case 2:
{
for(int i=0;i<h;i++)
{
int u=-random.nextInt()%g;
int v=-random.nextInt()%g;
int x=-random.nextInt()%g;
int y=-random.nextInt()%g;
char w=arr[random.nextInt(4)];
if(v!=0 && w=='/' )
System.out.println(u+"/"+v+" "+w+" "+x+"/"+y+"=");
else
System.out.println(u+"/"+v+" "+arr[random.nextInt(3)]+" "+x+"/"+y+"=");
}
break;
}
default:
System.out.print("输入的数有误,请输入1或2");
}
break;
}
default:
System.out.println("输入的数有误,请输入1--4的整数");
}
}
}
三、实现截图
四、PSP0级 记录表
一、项目计划总结:
周活动总结表
姓名:杨广鑫 日期:2015.3.13
日期 任务 | 听课 | 编写程序 | 阅读课本 | 准备考试 | 日总计 | ||
周日 | |||||||
周一 | |||||||
周二 | 100 | 60 | 20 | 180 | |||
周三 | 120 | 120 | |||||
周四 | 10 | 10 | |||||
周五 | 100 | 50 | 15 | 165 | |||
周六 | 45 | 45 | |||||
周总结 | 200 | 275 | 45 | 520 |
阶段时间和效率 周数(上一次周活动表的周数+1):
不包括上一周在内的累计时间
总计 | |||||||
平均 | |||||||
最大 | |||||||
最小 |
以前各周的累计时间
总计 | 200 | 275 | 45 | 520 | |||
平均 | 200 | 275 | 45 | 520 | |||
最大 | 200 | 275 | 45 | 520 | |||
最小 | 200 | 275 | 45 | 520 |
二、时间记录表:
学生 杨广鑫 日期 2015年3月13日
教师 王建民 课程 PSP
日期 | 开始时间 | 结束时间 | 中断时间 | 净时间 | 活动 | 备注 |
3.10 | 19:15 | 20:15 | 无 | 60 | 编程序 | 作业 |
3.11 | 14:30 | 21:14 | 有 | 120 | 编程序 | 作业 |
3.12 | 无 | |||||
3.13 | 18:50 | 19:40 | 无 | 50 | 完善程序 | 作业 |
3.14 | 9:10 | 9:55 | 无 | 45 | 完善程序 | 作业 |
三、缺陷记录日志:
学生 杨广鑫
日期 2015年3月13日
教员 王建民
程序号
日期 | 编号 | 类型 | 引入阶段 | 排除阶段 | 修复时间 | 修复缺陷 |
3.10 | 1 | a2 | 运行 | 运行之后马上 | 1min | |
在输入时未导入java.util.*。 | ||||||
3.10 | 2 | a3 | 编译 | 编译 | 1min | |
在判断if(ph[] == 1)条件时,将==写成= | ||||||
3.10 | 3 | B1 | 运行 | 运行之后 | 3min | |
在运行时,输出的结果虽然正确,但格式却不正确,行间距混乱 | ||||||
3.12 | 4 | a1 | 编译 | 编译 | 1min | |
漏掉; | ||||||
3.13 | 5 | B2 | 运行 | 运行之后 | 15min | |
在运行时,输出的格式不是按要求的输出的,原因是if语句插入的位置不正确 |
五、体会心得
在编程过程中出现很多问题。比如一个算法感觉逻辑上没问题,代码也没错误,但是程序结果就不按照自己的想法去实现;
另外对java编程还很不熟练,对于一些简单的函数还得查阅资料,才能写出。
由于原来是用c++写程序的,现在用java感觉一些编程有细微差距,还需要适应java。