import java.math.BigDecimal;
public class FiveMath {
//乘法
public static void createMultiplication() {
int one;
String two, three;
one = (int)(Math.random() * 10);
if (one < 6) { //小数乘小数
int num = (int)(Math.random() * 10);
if(num < 5) { //一位小数乘一位小数
if((int)(Math.random() * 10) < 5) {
two = String.format("%.1f", Math.random() * 100);
three = String.format("%.1f", Math.random() * 10);
} else {
two = String.format("%.1f", Math.random() * 10);
three = String.format("%.1f", Math.random() * 10);
}
} else if(num < 8) { //两位小数乘一位小数
two = String.format("%.2f", Math.random() * 10);
three = String.format("%.1f", Math.random() * 10);
} else { //一位小数乘两位小数
two = String.format("%.1f", Math.random() * 10);
three = String.format("%.2f", Math.random() * 10);
}
} else if (one < 8) { //小数乘整数
if((int)(Math.random() * 10) < 5) { //一位小数乘两位整数
two = String.format("%.1f", Math.random() * 10);
three = String.valueOf((int)(Math.random() * 99) + 1);
} else { //两位小数乘一位整数
two = String.format("%.2f", Math.random() * 10);
three = String.valueOf((int)(Math.random() * 9) + 1);
}
} else { //整数乘小数
if((int)(Math.random() * 10) < 5) { //两位整数乘一位小数
two = String.valueOf((int)(Math.random() * 99) + 1);
three = String.format("%.1f", Math.random() * 10);
} else { //一位整数乘两位小数
two = String.valueOf((int)(Math.random() * 9) + 1);
three = String.format("%.2f", Math.random() * 10);
}
}
System.out.println(two + "x" + three + "=");
}
//除法
public static void createDivision() {
int one;
String two, three;
one = (int)(Math.random() * 10);
if (one < 6) { //小数除小数
two = String.format("%.1f", Math.random() * 10);
three = String.format("%.1f", Math.random() * 10);
BigDecimal p1 = new BigDecimal(two);
BigDecimal p2 = new BigDecimal(three);
System.out.println(p1.multiply(p2).doubleValue() + "÷" + three + "=");
} else if (one < 8) { //小数除整数
if((int)(Math.random() * 10) < 5) {
two = String.format("%.1f", Math.random() * 10);
three = String.valueOf((int)(Math.random() * 99) + 1);
} else {
two = String.format("%.2f", Math.random() * 10);
three = String.valueOf((int)(Math.random() * 9) + 1);
}
BigDecimal p1 = new BigDecimal(two);
BigDecimal p2 = new BigDecimal(three);
System.out.println(p1.multiply(p2).doubleValue() + "÷" + three + "=");
} else { //整数除小数
if((int)(Math.random() * 10) < 5) {
two = String.valueOf((int)(Math.random() * 99) + 1);
three = String.format("%.1f", Math.random() * 10);
} else {
two = String.valueOf((int)(Math.random() * 9) + 1);
three = String.format("%.2f", Math.random() * 10);
}
BigDecimal p1 = new BigDecimal(two);
BigDecimal p2 = new BigDecimal(three);
System.out.println(p1.multiply(p2).doubleValue() + "÷" + three + "=");
}
}
//加法
public static void createAddition() {
int one;
String two, three;
one = (int)(Math.random() * 10);
if (one < 6) { //小数加小数
int num = (int)(Math.random() * 10);
if(num < 5) { //一位小数加一位小数
if((int)(Math.random() * 10) < 5) {
two = String.format("%.2f", Math.random() * 100);
three = String.format("%.1f", Math.random() * 10);
} else {
two = String.format("%.1f", Math.random() * 10);
three = String.format("%.1f", Math.random() * 10);
}
} else if(num < 8) { //两位小数加一位小数
two = String.format("%.2f", Math.random() * 10);
three = String.format("%.1f", Math.random() * 10);
} else { //一位小数加两位小数
two = String.format("%.1f", Math.random() * 10);
three = String.format("%.2f", Math.random() * 10);
}
} else if (one < 8) { //小数加整数
if((int)(Math.random() * 10) < 5) {
two = String.format("%.1f", Math.random() * 10);
three = String.valueOf((int)(Math.random() * 99) + 1);
} else {
two = String.format("%.2f", Math.random() * 10);
three = String.valueOf((int)(Math.random() * 9) + 1);
}
} else { //整数加小数
if((int)(Math.random() * 10) < 5) {
two = String.valueOf((int)(Math.random() * 99) + 1);
three = String.format("%.1f", Math.random() * 10);
} else {
two = String.valueOf((int)(Math.random() * 9) + 1);
three = String.format("%.2f", Math.random() * 10);
}
}
System.out.println(two + "+" + three + "=");
}
//减法
public static void createSubtraction() {
int one;
String two, three;
one = (int)(Math.random() * 10);
if (one < 6) { //小数减小数
int num = (int)(Math.random() * 10);
if(num < 5) { //一位小数减一位小数
if((int)(Math.random() * 10) < 5) {
two = three = String.format("%.2f", Math.random() * 100);
three = String.format("%.1f", Math.random() * 10);
} else {
two = String.format("%.1f", Math.random() * 10);
three = String.format("%.1f", Math.random() * 10);
}
} else if(num < 8) { //两位小数减一位小数
two = String.format("%.2f", Math.random() * 10);
three = String.format("%.1f", Math.random() * 10);
} else { //一位小数减两位小数
two = String.format("%.1f", Math.random() * 10);
three = String.format("%.2f", Math.random() * 10);
}
BigDecimal p1 = new BigDecimal(two);
BigDecimal p2 = new BigDecimal(three);
System.out.println(p2.add(p1).doubleValue() + "-" + three + "=");
} else if (one < 8) { //小数减整数
if((int)(Math.random() * 10) < 5) {
two = String.format("%.1f", Math.random() * 10);
three = String.valueOf((int)(Math.random() * 99) + 1);
} else {
two = String.format("%.2f", Math.random() * 10);
three = String.valueOf((int)(Math.random() * 9) + 1);
}
BigDecimal p1 = new BigDecimal(two);
BigDecimal p2 = new BigDecimal(three);
System.out.println(p2.add(p1).doubleValue() + "-" + three + "=");
} else { //整数减小数
if((int)(Math.random() * 10) < 5) {
two = String.valueOf((int)(Math.random() * 99) + 1);
three = String.format("%.1f", Math.random() * 10);
} else {
two = String.valueOf((int)(Math.random() * 9) + 1);
three = String.format("%.2f", Math.random() * 10);
}
BigDecimal p1 = new BigDecimal(two);
BigDecimal p2 = new BigDecimal(three);
System.out.println(p2.add(p1).doubleValue() + "-" + three + "=");
}
}
public static void main(String[] args) {
for(int i = 0; i < 20; i++) { //一共生成20道题
int num = (int)(Math.random() * 10);
if(num < 4) { //乘法占40%
createMultiplication();
} else if(num < 8) { //除法占40%
createDivision();
} else if(num < 9) { //加法占10%
createAddition();
} else { //减法占10%
createSubtraction();
}
}
}
}
小学五年级口算题(代码生成)
最新推荐文章于 2024-10-25 22:36:22 发布