标签:gen 规则 auto tin rand -- 不重复 util equals
1.刚开始是用随机函数生成随机数random.nextInt,然后按照老师的要求先输出30个题目,把运算符的随机生成放入到数字中去;然后再依次进行:
用数组存放题目,为了满足题目不重复的要求进行查找;最后就是真分数的运算,按照一定得规则对输出的运算数据进行约束即可,最后选择输
出的题目输出结果。
2.源代码:
package test;
import java.util.*;
public class siZe {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
int N;
int mid;
System.out.println("请输入题目的总数:");
N = scan.nextInt();
String[] timu = new String[N];//用于存储题目
if(N%2== 0)
mid = N/2;
else
mid = (N-1)/2;
for(int i = 0;i<mid;i++){
timu = zhengShu(N);
System.out.println(i+1 + ".) "+timu[i]);
}
for(int i = mid;i<N;i++){
timu = zhenFenshu(N);
System.out.println(i+1 + ".) "+timu[i]);
}
}
public static String[] zhengShu(int n){
String[] timu = new String[n];
String linshi = "";
int num1,num2,singal = 0;
int i = 0;//统计次数
int j = 0,k=0,l=0;
Random random = new Random();
while(i < n){
num1 = random.nextInt(100);
num2 = random.nextInt(100);
singal = random.nextInt(4);
if(singal == 0)
j=j+num1+num2;
linshi = ""+num1 + "+" + num2 + "=" + j;
if(singal == 1&&num1>num2)
l=l+num1-num2;
linshi = ""+num1 + "-" + num2 + "=" + l;
if(singal == 2)
linshi = ""+num1 + "*" + num2 + "=" + num1*num2;
if(singal == 3&&num2!=0&&num1%num2 == 0)
linshi = ""+num1 + "/" + num2 + "=" + num1/num2;
if(bChongFu(timu,linshi,i)){
timu[i] = linshi;
i++;
}
}
return timu;
}
public static String[] zhenFenshu(int n){
String[] timu = new String[n];
String linshi = "";
int num1,num2,singal, x , y = 0; //num1、num2为分子,a、b为分子
int i = 0;//统计次数
Random random = new Random();
while(i < n){
num1 = random.nextInt(100);
num2 = random.nextInt(100);
singal = random.nextInt(4);
x = random.nextInt(100);
y = random.nextInt(100);
if(singal == 0 && y!=0 && num1*y+num2*x<=x*y)
linshi = ""+num1 + "/" + x +" + "+ num2 + "/" + y + "=" + simpleNumber(num1*y+num2*x,x*y);
if(singal == 1 && y!=0 && num1*y+num2*x<=x*y)
linshi = ""+num1 + "/" + x +" - "+ num2 + "/" + y + "=" + simpleNumber(num1*y-num2*x,x*y);
if(singal == 2 && y!=0 && num1*num2<=x*y)
linshi = ""+num1 + "/" + x +" * "+ num2 + "/" + y + "=" +simpleNumber (num1*num2,x*y);
if(singal == 3 && y!=0 && num1*y<=num2*x)
linshi = ""+num1 + "/" + x + " / "+num2 + "/" + y + "=" + simpleNumber(num1*y,num2*x);
if(bChongFu(timu,linshi,i)){
timu[i] = linshi;
i++;
}
}
return timu;
}
public static String simpleNumber(int f1,int f2){
int maxYs = 1;
for(int i = f1;i>=1;i--){
if(f1%i==0 && f2%i==0){
maxYs = i;
break;
}
}
f1 = f1 / maxYs;
f2 = f2 / maxYs;
if(f1 == 0)
return "0";
else
return +f1+"/"+f2;
}
public static boolean bChongFu(String[] timu,String linshi,int n){
for(int i = 0;i<n;i++){
if(timu[i].equals(linshi))
return false;
}
return true;
}
}
3.
4.上课刚开始由于忘了随机函数的生成耽误了一些时间,然后就是没有想到把运算符的随机生成也加入到数字随机生成里,没有考虑完全。
标签:gen 规则 auto tin rand -- 不重复 util equals
原文地址:http://www.cnblogs.com/ming123/p/6511615.html