package operation;
import java.util.Random;
import java.util.Scanner;
public class operation {
public static int number = 0;//个数
public static int mult = 0;//操作数的个数
public static int bracket = 0;//操作符个数,最多不过五
public static int num = 0;//括号
public static int count = 0;//换行数
public static int max = 0;//范围
public static int division;//乘除
public static void main(String[] args) {
menu();
Random r = new Random();
int s = mudi(division);
int a;
int[] arr1 = new int[number * mult];//所有的数字
int[] arr2 = new int[number];
String[] arr3 = new String[number * (mult - 1)];//符号
String[] arr4 = new String[number];
int sum = 0;
//mult-1
if (bracket == 1) {
int symbol1 = 0;
int symbol2 = 0;
for (int i = 0; i < number; i++) {//for循环输出所有个数
count++;
symbol1 = r.nextInt(3);
symbol2 = r.nextInt(2);
if (symbol1 == 0) {
for (int j = 0; j < mult; j++) {//for循环输出公式,输出没有括号的公式
if (j == mult - 1) {
a = r.nextInt(max);
System.out.print(a + "=\t");
arr2[j] = a;
} else {
a = r.nextInt(max);
System.out.print(a);
arr4[j] = fuHao(s);
arr2[j] = a;
}
}
}else if (symbol1 == 1) {
for (int j = 0; j < mult; j++) {
if (j == mult - 1) {
a = r.nextInt(max);
System.out.print(a);
if (symbol2 == 1 && j == mult - 1) {
System.out.print(")");
}
System.out.print("=\t");
arr2[j] = a;
} else {
a = r.nextInt(max);
if(j == 0){
System.out.print("(");}
System.out.print(a);
if (symbol2 == 0 && j == mult - 2) {
System.out.print(")");
}
arr2[j] = a;
arr4[j] = fuHao(s);
}
}
}else if (symbol1 == 2) {
for (int j = 0; j < mult; j++) {
if (j == mult - 1) {
a = r.nextInt(max);
System.out.print(a);
if (symbol2 == 1 && j == mult - 1) {
System.out.print(")");
}
System.out.print("=\t");
arr2[j] = a;
} else {
a = r.nextInt(max);
if(j == 1)
{System.out.print("(");}
System.out.print(a);
if (symbol2 == 0 && j == mult - 3) {
System.out.print(")");
}
arr2[j] = a;
arr4[j] = fuHao(s);
}
}
}
for (int x = 0; x < number; x++) {
arr1[x + sum] = arr2[x];
arr3[x + sum] = arr4[x];
sum++;
}
if (count % num == 0)
System.out.println();
}
} else if (bracket == 0) {
for (int i = 0; i < number; i++) {//for循环输出所有个数
count++;
for (int j = 0; j < mult; j++) {//for循环输出公式
if (j == mult - 1) {
a = a = r.nextInt(max);
System.out.print(a + "=\t");
} else {
a = r.nextInt(max);
System.out.print(a);
fuHao(s);
}
}
if (count % num == 0)
System.out.println();
}
}
System.out.println("重复率为0");
//做题
//arr1[] 是所有的数字
//arr1[]/mult是number
//arr3[] 符号
int[] answer = new int[number];
for (int q = 0; q < number; q = q+mult) {//实现答案的输出
int shu = 0;
//arr1[0] arr3[0] arr1[1] arr3[1] arr1[2] arr3[2] arr1[3] = answer[0];
for (int i = 0; i < mult; i++) {
if (arr3[i + shu] == "+") answer[i] = arr1[i] + answer[i];
if (arr3[i + shu] == "-") answer[i] = arr1[i] - answer[i];
if (arr3[i + shu] == "*") answer[i] = arr1[i] * answer[i];
if (arr3[i + shu] == "/") answer[i] = arr1[i] / answer[i];
}
shu++;
}
}
private static int mudi(int division) {
int s = 0;
if(division == 0){//调整是否有乘除法
division = 2;
}else if(division == 1)
{
division=4;
}
s = division;
return s;
}
//System.out.print(arr2[i] + "=\t");
private static void menu() { //菜单
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要生成题目的个数:");
number = sc.nextInt();//个数
System.out.println("请输入每行的题数:");
num = sc.nextInt();//每行题目的个数
System.out.println("请输入操作数的个数:");
mult = sc.nextInt();//操作符个数
System.out.println("请输入是否有乘法除法,0是没有,1是有:");
division = sc.nextInt();
System.out.println("请选择是否有括号,0是没有,1是有:");
bracket = sc.nextInt();//括号
System.out.println("请输入出题数值的范围:");
max = sc.nextInt();//范围
}
public static String fuHao(int division) { //调用符号
Random r = new Random();
int a=r.nextInt(division)+1;
String zz = "";
if(a==1) {System.out.print("+");
zz = "+";}
if(a==2) {System.out.print("-");
zz = "-";}
if(a==3) {System.out.print("*");
zz = "*";}
if(a==4) {System.out.print("/");
zz = "/";}
return zz;
}
}
![](/i/l/?n=22&i=blog/2913371/202209/2913371-20220930165820916-587514381.png)
标签:int,四则运算,System,nextInt,测试,print,mult,out From: https://www.cnblogs.com/yzx-sir/p/16745504.html