2022.9.9四则运算
有很多小问题还没解决,思路,结构很乱。。。
随机数参考了~四则运算 9.29 - ***Pepsi*** - 博客园 (cnblogs.com)
package temomomomo;
import java.util.Scanner;
import java.util.Random;
public class sizeyunsuan {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
String[] operator1= new String[] {"+","-","x","÷"};
String[] operator2=new String[] {"+","-"};
System.out.println("输入题目数量:");
int num;
num=in.nextInt();
System.out.println("打印方式:");
int pri;
pri=in.nextInt();
System.out.println("操作数个数:");
int op,opp; //一开始以为操作数是运算符号的个数,但是可以转换一下运算符号的个数opp=操作数op-1,后面的op用opp取代掉
op=in.nextInt();
opp=op-1;
System.out.println("是否有乘除法(Y/N):");
String ch;
ch=in.next();
System.out.println("是否需要括号(Y/N):");
String kuohao;
kuohao=in.next();
System.out.println("取值范围:");
int min,max,m;
min=in.nextInt();
max=in.nextInt();
m=max-min+1;
int[] numbers=new int [m];
int j=min;
for(int i=0;i<m;i++) {
numbers[i]=j;
j++;
}
Random r=new Random();
int n=0;
for(int i=0;i<num;i++) {
if(kuohao.equals("Y")) {
int weizhi=r.nextInt(opp)+1;
int find=0;
for(int k=0;k<opp;k++) {
if(weizhi-1==k) {
System.out.print("(");
find=1;
if(ch.equals("N")==true) {
System.out.print(numbers[r.nextInt(m)]+operator2[r.nextInt(2)]);
}
else {
System.out.print(numbers[r.nextInt(m)]+operator1[r.nextInt(4)]);
}
}
else {
System.out.print(numbers[r.nextInt(m)]);
if(find==1) {
System.out.print(")");find=0;
}
if(ch.equals("N")==true) {
System.out.print(operator2[r.nextInt(2)]);
}
else {
System.out.print(operator1[r.nextInt(4)]);
}
}
}
System.out.print(numbers[r.nextInt(m)]);
if(find==1) {
System.out.print(")");
}
System.out.print("=");
}
else {
for(int k=0;k<opp;k++) {
if(ch.equals("N")==true) {
System.out.print(numbers[r.nextInt(m)]+operator2[r.nextInt(2)]);
}
else {
System.out.print(numbers[r.nextInt(m)]+operator1[r.nextInt(4)]);
}
}
System.out.print(numbers[r.nextInt(m)]+"=");
}
n++;
if(n%pri==0)System.out.print("\n");
else System.out.print("\t");
}
}
}
修改opp后截图: