题目要求:随机产生30个的四则运算
- 题目避免重复;
- 可定制(数量/打印方式);
- 可以控制下列参数;
是否有乘除法、数值范围、加减有无负数、除法有无余数、是否支持分数(真分数,假分数,...);(5选4)
设计思想:
在上一篇已经写出了,在这里就不多做介绍。重点要说的是我在编程过程中遇到的问题。我用的JAVA技术,首先考虑是if条件语句。不断的向下分层。第一个问题就是在打印题目的过程中无法确定每行打印题目数。后来研究了下。才发现应该打印题目不换行。根据打印的题目数换行。
源代码:
import java.util.Random;
import java.util.Scanner;
public class main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int a,b,c,m,n,min,max;
Scanner sc = new Scanner(System.in);
System.out.println("请输入要打印题目的数量和每行要印的的一模数量:");
m = sc.nextInt();
n = sc.nextInt();
System.out.println("请输入数值的范围:");
min = sc.nextInt();
max = sc.nextInt();
System.out.println("请输入是否有乘数法:(有输入1.反之输入2)");
a = sc.nextInt();
System.out.println("请输入加减法是否有负数:(有输入1.反之输入2)");
b = sc.nextInt();
System.out.println("请输入除法是否有余数:(有输入1.反之输入2)");
c = sc.nextInt();
sc.close();
if(a==1)//有乘数法
{
if(b==1)//有乘数法。加减法有负数
{
if(c==1)//有乘数法。加减法有负数。除法有余数
{
for(int i=0;i<m;i++)
{
Random rand = new Random();
int firstnum = rand.nextInt(max)+min;
int secondnum = rand.nextInt(max)+min;
int num = rand.nextInt(4)+1;
if(num==1)
System.out.print(firstnum+" + "+secondnum+" = ");
if(num==2)
System.out.print(firstnum+" - "+secondnum+" = ");
if(num==3)
System.out.print(firstnum+" * "+secondnum+" = ");
if(num==4)
System.out.print(firstnum+" / "+secondnum+" = ");
if((i+1)%n==0){System.out.println(" ");}
}
}
if(c==2)//有乘数法。加减法有负数。除法无余数
{
for(int i=0;i<m;i++)
{
Random rand = new Random();
int firstnum = rand.nextInt(max)+min;
int secondnum = rand.nextInt(max)+min;
int num = rand.nextInt(4)+1;
if(num==1)
System.out.print(firstnum+" + "+secondnum+" = ");
if(num==2)
System.out.print(firstnum+" - "+secondnum+" = ");
if(num==3)
System.out.print(firstnum+" * "+secondnum+" = ");
if(num==4){
if(firstnum%secondnum!=0)
{
i=i-1;
}
else{System.out.print(firstnum+" / "+secondnum+" = ");}
if((i+1)%n==0){System.out.println(" ");}
}
}
}
}
if(b==2)//有乘数法。加减法无负数
{
if(c==1)//有乘数法。加减法无负数。除法有余数
{
for(int i=0;i<m;i++)
{
Random rand = new Random();
int firstnum = rand.nextInt(max)+min;
int secondnum = rand.nextInt(max)+min;
int num = rand.nextInt(4)+1;
if(num==1)
System.out.print(firstnum+" + "+secondnum+" = ");
if(num==2)
{
int l;
l=firstnum-secondnum;
if(l<0){
i=i-1;
}
if(l>=0){
System.out.print(firstnum+" - "+secondnum+" = ");
}
}
if(num==3)
System.out.print(firstnum+" * "+secondnum+" = ");
if(num==4)
System.out.print(firstnum+" / "+secondnum+" = ");
if((i+1)%n==0){System.out.println(" ");}
}
}
if(c==2)//有乘数法。加减法无负数。除法无余数
{
for(int i=0;i<m;i++)
{
Random rand = new Random();
int firstnum = rand.nextInt(max)+min;
int secondnum = rand.nextInt(max)+min;
int num = rand.nextInt(4)+1;
if(num==1)
System.out.print(firstnum+" + "+secondnum+" = ");
if(num==2)
{
int l;
l=firstnum-secondnum;
if(l<0){
i=i-1;
}
if(l>=0){
System.out.print(firstnum+" - "+secondnum+" = ");
}
}
if(num==3)
System.out.print(firstnum+" * "+secondnum+" = ");
if(num==4){
if(firstnum%secondnum!=0)
{
i=i-1;
}
else{System.out.print(firstnum+" / "+secondnum+" = ");}
if(i%n==0){System.out.print("\n");}
}
}
}
}
if(a==2)//无乘数法
{
if(b==1)//没有乘数法加减法有负数
{
if(c==1||c==2){
for(int i=0;i<m;i++)
{
Random rand = new Random();
int firstnum = rand.nextInt(max)+min;
int secondnum = rand.nextInt(max)+min;
int num = rand.nextInt(2)+1;
if(num==1)
System.out.print(firstnum+" + "+secondnum+" = ");
if(num==2)
System.out.print(firstnum+" - "+secondnum+" = ");
if((i+1)%n==0){System.out.println(" ");}
}
}
}
if(b==2)//没有乘数法,加减法无负数
{
if(c==1||c==2){
for(int i=0;i<m;i++)
{
Random rand = new Random();
int firstnum = rand.nextInt(max)+min;
int secondnum = rand.nextInt(max)+min;
int num = rand.nextInt(2)+1;
if(num==1){
System.out.print(firstnum+" + "+secondnum+" = ");}
if(num==2)
{
int l;
l=firstnum-secondnum;
if(l<0){
i=i-1;
}
if(l>=0){
System.out.print(firstnum+" - "+secondnum+" = ");
}
}
if((i+1)%n==0){System.out.println("");}
}
}
}
}
}
}
}
运行结果:
编程总结:
怎么说呢,刚开始看到这个题目的时候觉得很难,但是根据老师的教的方法,一点点的思考,设计编程思想,看似很难的问题迎刃而解。以后每一个程序写之前,我都要先写设计思想。
时间表: