JAVA随机生成四则运算

import java.sql.SQLOutput;
import java.util.Random;
import java.util.Scanner;

public class newNum {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num1;
System.out.println("请输入操作数的个数:1(2个), 2(3个)");
num1 = sc.nextInt();

switch (num1) {

case 1:
case1();
break;
case 2:
case2();
break;


}


}

public static void myInsert(StringBuffer str, int num) {

Random rand = new Random();
while(true) {
int i = rand.nextInt((num - 1) * 3 - 2);
if (i % 3 == 0) {

str.insert(i, "(");
str.insert(i + 6, ")");
break;

}
}

}

public static void case1() {
Scanner sc = new Scanner(System.in);
int num2;
System.out.println("是否加入乘除:1(是), 2(否)");
num2 = sc.nextInt();
System.out.println("请输入你要打印的题数:");
int m = sc.nextInt();
Random rand = new Random();
int i, j, k;
String[] s = new String[m];
for(int o = 0; o < m; o++) {
i = rand.nextInt(4);
j = rand.nextInt(100);
k = rand.nextInt(100);
char Fuhao;
if(i == 0) {

Fuhao = '+';

}else if (i == 1) {
Fuhao = '-';

}else if (i == 2) {

Fuhao = '*';

}else if (i == 3) {

Fuhao = '/';
}
StringBuffer str = new StringBuffer();
s[o] = str.append(i).append(j).append(k).toString();


int sum = 0;
for(int q = 0; q < s.length; q++) {

if(s[o].equals(s[q])) {
sum++;
}

}

if(sum == 1) {

if(i == 0) {

System.out.println(j + "+" + k + "=");
}
if(i == 1) {

System.out.println(j + "-" + k + "=");
}
if(i == 2) {

if(num2 == 1) {
System.out.println(j + "*" + k + "=");

}else {

o--;
continue;
}
}
if(i == 3) {
if(num2 == 1) {
if (k == 0) {

o--;
continue;

} else {
System.out.println(j + "/" + k + "=");
}
}else {

o--;
continue;

}
}

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

}

}

}

public static void case2() {

Scanner sc = new Scanner(System.in);
int num2, num3, num4;
System.out.println("请输入操作数的范围:");
num4 = sc.nextInt();
System.out.println("是否加入乘除:1(是), 2(否)");
num2 = sc.nextInt();
System.out.println("是否加入括号:1(是), 2(否)");
num3 = sc.nextInt();
System.out.println("请输入你要打印的题数:");
int m = sc.nextInt();
Random rand = new Random();
int i, j, k, l,u;
String[] s = new String[m];
for(int o = 0; o < m; o++) {
if(num2 == 2) {
i = rand.nextInt(2);
u = rand.nextInt(2);
}else {
i = rand.nextInt(4);
u = rand.nextInt(4);
}
l = rand.nextInt(num4);
j = rand.nextInt(num4);
k = rand.nextInt(num4);
String Fuhao = "", Fuhao2 = "";
if(i == 0) {

Fuhao = "+";

}else if (i == 1) {
Fuhao = "-";

}else if (i == 2) {

Fuhao = "*";

}else if (i == 3) {

Fuhao = "/";
}
if(u == 0) {

Fuhao2 = "+";

}else if (u == 1) {
Fuhao2 = "-";

}else if (u == 2) {

Fuhao2 = "*";

}else if (u == 3) {

Fuhao2 = "/";
}
StringBuffer str = new StringBuffer();
StringBuffer str1 = new StringBuffer();
s[o] = str.append(i).append(j).append(k).toString();

str1.append(l).append(Fuhao).append(j).append(Fuhao2).append(k).append("=");

int sum = 0;
for(int q = 0; q < s.length; q++) {

if(s[o].equals(s[q])) {
sum++;
}

}




if(sum == 1) {


if(num3 == 1) {

/*myInsert(str1, 3);
String str4 = str1.toString();
System.out.println(str4);
*/
int i3 = rand.nextInt(2);

if(i3 == 0) {

System.out.println("(" +l + Fuhao + j + ")" + Fuhao2 + k + "=");

}
if(i3 == 1) {

System.out.println(l + Fuhao + "(" +j + Fuhao2 + k + ")");

}


}else {

System.out.println(l + Fuhao + j + Fuhao2 + k + "=");

}


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

}

}

}

}
posted @   哈喽伍六柒  阅读(256)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示