一、题目: 像二柱子那样,花二十分钟写一个能自动生成三十道小学四则运算题目的 “软件”,要求:除了整数以外,还要支持真分数的四则运算(需要验证结果的正确性)、题目避免重复、可定制出题的数量。(有能力者改编成网页版)
1.设计思想:了解题目要求,进行相关的定义与程序编写,再进行验证。
2.源代码:
//韩迪的出题算法
//2017/3/6
package dijia;
import java.util.Random;
import java.util.Scanner;
public class test1 {
public static void main(String[] args) {
System.out.println("整数运算请输入1;分数预算请输入2");
Random random = new Random();
Scanner scanner = new Scanner(System.in);
int bb = scanner.nextInt();
if(bb == 1)
{
System.out.println("请输入题目的个数:");
int M = scanner.nextInt();
//定义变量
int m=0;
//创建M个数组储存M道题
int [][] a = new int[M][4];
//产生30道题目
for(int i = 0;i < M;i++)
{
a[i][0] = random.nextInt(100);
a[i][2] = random.nextInt(100);
a[i][1] = random.nextInt(4);
}
//排除重复的题目
while(m <= (M - 1)*M/2)
{
for(int j = 0;j < M;j++)
{
for(int k = j+1;k < M;k++)
{
m++;
if((a[j][0] == a[k][0] && a[j][1] == a[k][1] && a[j][2] == a[k][2]) || (a[j][1] == 3 && a[j][2] == 0))
{
int b = (int)Math.random()*100;
int c = (int)Math.random()*100;
int d = (int)Math.random()*4;
a[k][0] = b;
a[k][1] = d;
a[k][2] = c;
}
}
}
}
//输出题目
for(int i = 0;i < M;i++)
{
if(a[i][1] == 0)
{
System.out.println(a[i][0]+"+"+a[i][2]+"=");
}
if(a[i][1] == 1)
{
System.out.println(a[i][0]+"-"+a[i][2]+"=");
}
if(a[i][1] == 2)
{
System.out.println(a[i][0]+"*"+a[i][2]+"=");
}
if(a[i][1] == 3)
{
System.out.println(a[i][0]+"/"+a[i][2]+"=");
}
}
}
//分数运算
if(bb == 2)
{
System.out.println("请输入题目的个数:");
int MM = scanner.nextInt();
int nn = 0;
//定义相关数组
int a1[] = new int [MM];//储存第一个分数的分子
int b1[] = new int [MM];//储存第二个分数的分子
int a2[] = new int [MM];//储存第一个分数的分母
int b2[] = new int [MM];//储存第二个分数的分母
int cc[] = new int [MM];//储存四则运算符号
char dd[] ={'+','-','*','/'};
for(int i = 0;i < MM;i++)
{
a1[i] = random.nextInt(100);
a2[i] = random.nextInt(100);
b1[i] = random.nextInt(100);
b2[i] = random.nextInt(100);
cc[i] = random.nextInt(4);
}
//排除的分数题目
while(nn <= MM*(MM-1)/2)
{
for(int iii = 0;iii < MM;iii++)
{
nn++;
for(int ii = iii+1;ii < MM;ii++)
{
nn++;
if( (cc[ii] == 3 && b1[ii] == 0) || b2[ii] == 0 || a2[ii] == 0 || (a1[ii] >= a2[ii]) || (b1[ii] >= b2[ii]) || (a1[ii] == a1[iii] && b1[ii] == b1[iii] && a2[ii] == a2[iii] && b2[ii] == b2[iii] && cc[ii] == cc[iii]))
{
a1[ii] = random.nextInt(100);
a2[ii] = random.nextInt(100);
b1[ii] = random.nextInt(100);
b2[ii] = random.nextInt(100);
cc[ii] = random.nextInt(4);
}
}
}
}
//输出题目
for(int jj = 0;jj < MM;jj++)
{
System.out.println("("+a1[jj]+"/"+a2[jj]+")"+dd[cc[jj]]+"("+b1[jj]+"/"+b2[jj]+")"+"=");
}
}
}
}
3.截图:
<1>
<2>