生成小学加减口算题JAVA算法
2016-06-21 06:19
786 查看
生成小学加减口算题JAVA算法
package com.jianchi.fsp.generationchildrenarithmetic;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* Created by fsp on 16-6-20.
*/
public class GenHelper implements Serializable {
//每道题的数字数,比如 2 , 3 ,4
public int numberNum;
//生成题的数量
public int createCount;
//运算类型
public String calculationMethod;
//数值范围
public int resultRange;
public List<String> make() {
Random r = new Random();
Boolean[] methods = new Boolean[numberNum - 1];
int[] nums = new int[numberNum];
List<String> l = new ArrayList<>();
for (int count = 0; count < createCount; count++) {
for (int i = 0; i < methods.length; i++) {
if (calculationMethod.equals("混合"))
methods[i] = r.nextInt(2) == 0;//生成 0 , 1, 0 为 true 为 加法
else if (calculationMethod.equals("加法"))
methods[i] = true;//生成 0 , 1
else if (calculationMethod.equals("减法"))
methods[i] = false;//生成 0 , 1
}
while (true) {
for (int i = 0; i < nums.length; i++) {
nums[i] = r.nextInt(resultRange + 1);
}
if (test(methods, nums)) {
l.add(String.valueOf(count + 1) + ". " + create(methods, nums));
break;
}
}
}
return l;
}
String create(Boolean[] methods, int[] nums) {
StringBuilder sb = new StringBuilder();
sb.append(nums[0]);
for (int i = 0; i < methods.length; i++) {
sb.append(" ").append(methods[i] ? "+" : "-").append(" ").append(nums[i + 1]);
}
sb.append(" ").append("=");
return sb.toString();
}
boolean test(Boolean[] methods, int[] nums) {
int d = nums[0];
for (int i = 0; i < methods.length; i++) {
d = methods[i] ? d + nums[i + 1] : d - nums[i + 1];
if (d < 0 || d > resultRange)
return false;
}
return true;
}
}
项目源码地址 https://code.csdn.net/do168/generationchildrenarithmetic
package com.jianchi.fsp.generationchildrenarithmetic;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* Created by fsp on 16-6-20.
*/
public class GenHelper implements Serializable {
//每道题的数字数,比如 2 , 3 ,4
public int numberNum;
//生成题的数量
public int createCount;
//运算类型
public String calculationMethod;
//数值范围
public int resultRange;
public List<String> make() {
Random r = new Random();
Boolean[] methods = new Boolean[numberNum - 1];
int[] nums = new int[numberNum];
List<String> l = new ArrayList<>();
for (int count = 0; count < createCount; count++) {
for (int i = 0; i < methods.length; i++) {
if (calculationMethod.equals("混合"))
methods[i] = r.nextInt(2) == 0;//生成 0 , 1, 0 为 true 为 加法
else if (calculationMethod.equals("加法"))
methods[i] = true;//生成 0 , 1
else if (calculationMethod.equals("减法"))
methods[i] = false;//生成 0 , 1
}
while (true) {
for (int i = 0; i < nums.length; i++) {
nums[i] = r.nextInt(resultRange + 1);
}
if (test(methods, nums)) {
l.add(String.valueOf(count + 1) + ". " + create(methods, nums));
break;
}
}
}
return l;
}
String create(Boolean[] methods, int[] nums) {
StringBuilder sb = new StringBuilder();
sb.append(nums[0]);
for (int i = 0; i < methods.length; i++) {
sb.append(" ").append(methods[i] ? "+" : "-").append(" ").append(nums[i + 1]);
}
sb.append(" ").append("=");
return sb.toString();
}
boolean test(Boolean[] methods, int[] nums) {
int d = nums[0];
for (int i = 0; i < methods.length; i++) {
d = methods[i] ? d + nums[i + 1] : d - nums[i + 1];
if (d < 0 || d > resultRange)
return false;
}
return true;
}
}
项目源码地址 https://code.csdn.net/do168/generationchildrenarithmetic
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树