日常Java(测试 (二柱)修改版)2021/9/22

题目:

一家软件公司程序员二柱的小孩上了小学二年级,老师让家长每天出30道四则运算题目给小学生做。

二柱一下打印出好多份不同的题目,让孩子做了。老师看了作业之后,对二柱赞许有加。别的老师闻讯, 问二柱能否扩大他的影响力,编个软件,给二年级到四年级都用,多了一些小小的要求: 

1、题目避免重复(第二阶段)

2、可定制(数量/打印方式):输入大的数量值,测试一下系统是否崩溃,反向查找系统是否优化的余地

3、定制操作数的个数

4、定制是否有乘除法

5、定制是否有括号(随机加入)

6 、定制数值范围(确定操作数的取值范围)

 

建立了s包,建立了number类,创建了主函数main();,以及三个函数o1();o2();o3();

package s;

import java.util.Random;
import java.util.Scanner;
import java.util.*;

public class number {
public static void main(String[] args)
{
boolean flag = true;
while(flag) {
System.out.println("请选择功能(1、2、3):");
Scanner sc = new Scanner(System.in);
int z = sc.nextInt();
switch(z) {
case 1:
o1();break;
case 2:
o2();break;
case 3:
o3();break;
default:System.out.println("没有此功能。");break;
}
}
}

public static void o1()//第一阶段
{
Random random = new Random();

Scanner sc = new Scanner(System.in);
System.out.println("请输入打印的题数:");
int m = sc.nextInt();

for(int o = 1; o <= m; o++) {
int a = random.nextInt(4);
int b = random.nextInt(100);
int c = random.nextInt(100);
switch(a)
{
case 0:

System.out.println(b+ "+" +c +"=" );
break;
case 1:

System.out.println(b+ "-" +c +"=" );
break;
case 2:

System.out.println(b+ "*" +c +"=" );
break;
case 3:

System.out.println(b+ "/" +c +"=" );
break;

default:
break;
}
}

}

public static void o2()//第二阶段
{
Random random = new Random();

Scanner sc = new Scanner(System.in);
System.out.println("请输入打印的题数:");
int m = sc.nextInt();

StringBuffer str = new StringBuffer();
String[] s = new String[m];
s[0] = str.toString();

for(int o = 0; o < m; o++) {

int a = random.nextInt(4);
int b = random.nextInt(100);
int c = random.nextInt(100);

str.append(a).append(b).append(c);
StringBuffer str2 = new StringBuffer();
s[o] = str2.append(a).append(b).append(c).toString();

int sum = 0;
for(int q = 0; q < s.length; q++) {
if(s[o].equals(s[q])) {
sum++;
}
}

if(sum == 1)
{
if(a == 0) {

System.out.println(b + "+" + c + "=");
}
if(a == 1) {

System.out.println(b + "-" + c + "=");
}
if(a == 2) {

System.out.println(b + "*" + c + "=");
}
if(a == 3) {

System.out.println(b + "/" + c + "=");
}

}else if(sum != 1){
o--;
continue;

}
}
}

public static void o3()//第三阶段
{
Scanner sc = new Scanner(System.in);
System.out.println("请输入打印的题数:");
int g = sc.nextInt();

System.out.println("有无乘除法,请输入y或n:");
String g1 = sc.next();

System.out.println("是否有括号,请输入y或n:");
String g2 = sc.next();

System.out.println("请输入操作个数(2-4):");
int g3 = sc.nextInt();

System.out.println("请输入操作数范围【50-200】:");
int g4 = sc.nextInt();

Random random = new Random();
int a = random.nextInt(4);
int a0 = random.nextInt(g3)+2;//操作数个数(2-4)

StringBuffer str = new StringBuffer();//动态数组
String[] s = new String[g];
s[0] = str.toString();//返回对象值

for( int i = 0; i < g; i++) {
a = random.nextInt(4);
int a1 = random.nextInt(g4);
int a2 = random.nextInt(g4);
int a3 = random.nextInt(g4);
int a4 = random.nextInt(g4);

if(g4 >=50 && g4 <=200){
a1 = random.nextInt(g4);
a2 = random.nextInt(g4);
a3 = random.nextInt(g4);
a4 = random.nextInt(g4);
}else {
System.out.println("操作数超出范围。");
}

if(g3 == 2) {
str.append(a).append(a1).append(a2);//添加数据
}else if(g3 ==3) {
str.append(a).append(a1).append(a2).append(a3);//添加数据
}else {
str.append(a).append(a1).append(a2).append(a3).append(a4);//添加数据
}

StringBuffer str2 = new StringBuffer();
if(g3 == 2) {
s[i] = str2.append(a).append(a1).append(a2).toString();
}else if(g3 == 3){
s[i] = str2.append(a).append(a1).append(a2).append(a3).toString();
}else {
s[i] = str2.append(a).append(a1).append(a2).append(a3).append(a4).toString();
}

int sum = 0;
for(int q = 0; q < s.length; q++)
{if(s[i].equals(s[q])) {sum++;}}

if(g3 == 2) {
if(g1.equals("n")) {
if(sum == 1)
{
if(a == 0) {System.out.println(a1 + "+" + a2 + "=");}
if(a == 1) {System.out.println(a1 + "-" + a2 + "=");}
if(a == 2) {i--;continue;}
if(a == 3) {i--;continue;}
}else if(sum != 1){i--;continue;}
}else if(g1.equals("y")) {
if(sum == 1)
{
if(a == 0) {System.out.println(a1 + "+" + a2 + "=");}
if(a == 1) {System.out.println(a1 + "-" + a2 + "=");}
if(a == 2) {System.out.println(a1 + "*" + a2 + "=");}
if(a == 3) {System.out.println(a1 + "/" + a2 + "=");}
}else if(sum != 1){i--;continue;}
}
}else if(g3 == 3) {
if(g1.equals("n")) {
if(sum == 1)
{
if(a == 0) {System.out.println(a1 + "-" +"("+ a2 +"+"+a3+")"+ "=");}
if(a == 1) {System.out.println("("+a1 + "-" + a2 +")"+"+"+a3+ "=");}
if(a == 2) {System.out.println("("+a1 + "+" + a2 +")"+"-"+a3+ "=");}
if(a == 3) {System.out.println(a1 + "+" +"("+ a2 +"-"+a3+")"+ "=");}
}else if(sum != 1){i--;continue;}
}else if(g1.equals("y")) {
if(sum == 1)
{
if(a == 0) {System.out.println("("+a1+"*" + a2+")"+"+" + a3 +"=");}
if(a == 1) {System.out.println(a1+"-" +"("+ a2+"/" + a3 +")"+"=");}
if(a == 2) {System.out.println(a1+"-" +"("+ a2+"*" + a3 +")"+"=");}
if(a == 3) {System.out.println("("+a1+"/" + a2+")"+"+" + a3 +"=");}
}else if(sum != 1){i--;continue;}
}
}else if(g3 == 4){
if(g1.equals("n")) {
if(sum == 1)
{
if(a == 0) {System.out.println(a1+"+" + "("+ a2+"+" +a3 +")"+"+" +a4+ "=");}
if(a == 1) {System.out.println(a1+"-" + "("+ a2+"+" +a3 +")"+"-" +a4+ "=");}
if(a == 2) {System.out.println(a1+"-" + a2+"-" + "("+a3 +"-" +a4+")"+ "=");}
if(a == 3) {System.out.println("("+a1+"+" + a2+")"+"-" + a3 +"+" +a4+ "=");}
}else if(sum != 1){i--;continue;}
}else if(g1.equals("y")) {
if(sum == 1)
{
if(a == 0) {System.out.println(a1+"+" + a2+"-" + "("+a3 +"*" +a4+")"+ "=");}
if(a == 1) {System.out.println(a1+"-" +"("+ a2+"/" + a3 +")"+"-" +a4+ "=");}
if(a == 2) {System.out.println("("+a1+"*" + a2+")"+"+" + a3 +"-" +a4+ "=");}
if(a == 3) {System.out.println("("+a1+"/" + a2+")"+"-" +"("+ a3 +"*" +a4+")"+ "=");}
}else if(sum != 1){i--;continue;}
}
}else {
System.out.println("操作个数超出范围。");
}
}

}
}

上一篇:程序员法律知识


下一篇:es6扩展运算符