标签:分享 efi 上传 pack 好的 重要 mat ips 最好
package softwar;
import java.io.*;
import java.util.Random;
import java.util.Scanner;
static int Number;//题目数
static int operator;//是否有乘除运算!
static int Maxnumber;//最大数
static int haveBrackets;//是否有括号
static int haveFloat;//是否有小数
public static void main(String[] args) throws Exception{//抛出某种I/O异常
Print();
System.out.print("需要屏幕输出,输入1;用文件输出,输入0:");
Scanner input=new Scanner(System.in);
int haveFile=input.nextInt();
if(haveFile==1)
PrintQuestionsScreen(Number,operator, Maxnumber, haveBrackets, haveFloat);//屏幕输出
else
PrintQuestionsFile(Number,operator, Maxnumber, haveBrackets, haveFloat);//文件输出
}
public static void Print() {
//功能选择菜单
Scanner input=new Scanner(System.in);
System.out.print("请输入题目数量: ");
Number=input.nextInt();
if(Number<1) {
System.out.print("输入错误\n");
return;
}
System.out.println();
System.out.print("是否需要乘除,需要请输入1,不需要请输入0: ");
operator=input.nextInt();
if(operator!=1&&operator!=0) {
System.out.print("输入错误\n");
return;
}
System.out.println();
System.out.print("请输入最大数: ");
Maxnumber=input.nextInt();
if(Maxnumber<1) {
System.out.print("输入错误\n");
return;
}
System.out.println();
System.out.print("是否需要小数,需要请输入1,不需要请输入0: ");
haveFloat=input.nextInt();
if(haveFloat!=1&&haveFloat!=0) {
System.out.print("输入错误\n");
return;
}
System.out.println();
System.out.print("是否需要括号,需要请输入1,不需要请输入0: ");
haveBrackets=input.nextInt();
if(haveBrackets!=1&&haveBrackets!=0) {
System.out.print("输入错误\n");
return;
}
}
public static void PrintQuestionsScreen(int Number,int operator,int Maxnumber,int haveBrackets,int haveFloat) {
int[] a=new int[10];//随机整数
float[] a1=new float[10];//随机小数
int c;
long t = System.currentTimeMillis();
String[] b={"+","-","*","/"};//运算符
Random random=new Random(t);//以随机时间作为种子创建一个Random对象
for(int i=0;i<Number;i++)
{
int n=random.nextInt(4)+2;//随机运算个数
if(haveBrackets==0) {
for(int j=0;j<n;j++) {
if(haveFloat==0)
a[j]=random.nextInt(Maxnumber-1)+1;
else {
int d=random.nextInt(2)+1;
a[j]=random.nextInt(Maxnumber-1)+1;
a1[j]=(float)(a[j]/Math.pow(10, d));
}
}
for(int k=0;k<n-1;k++) {
if(operator==0)
c=random.nextInt(2);//随机运算符
else
c=random.nextInt(4);
if(haveFloat==0)
System.out.print(a[k]+b[c]);
else
System.out.print(a1[k]+b[c]);
}
if(haveFloat==0)
System.out.println(a[n-1]+"=");
else
System.out.println(a1[n-1]+"=");
}
if(haveBrackets==1) {
for(int j=0;j<n;j++) {
if(haveFloat==0)
a[j]=random.nextInt(Maxnumber-1)+1;
else {
int d=random.nextInt(2)+1;
a[j]=random.nextInt(Maxnumber-1)+1;
a1[j]=(float)(a[j]/Math.pow(10, d));
}
}
int c1=random.nextInt(n-1);
int c2=random.nextInt(n-c1-1)+1;
for(int k=0;k<n-1;k++) {
if(operator==0)
c=random.nextInt(2);
else
c=random.nextInt(4);
if(c1==k)
System.out.print("(");//随机左括号产生的位置
if(haveFloat==0)
System.out.print(a[k]);
else
System.out.print(a1[k]);
if(c2+c1==k)
System.out.print(")");//随机右括号产生的位置
System.out.print(b[c]);
}
if(haveFloat==0)
System.out.print(a[n-1]);
else
System.out.print(a1[n-1]);
if(c1+c2==n-1)
System.out.print(")");
System.out.print("=\n");
}
}
}
标签:分享 efi 上传 pack 好的 重要 mat ips 最好
原文地址:https://www.cnblogs.com/fgh19970720/p/8836226.html