首页 > 其他 > 详细

随机生成四则运算并答题

时间:2022-05-27 19:43:02      阅读:4      评论:0      收藏:0      [点我收藏+]

package az;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class Main1 {
static int aa1=0;
static Scanner sc=new Scanner(System.in);
static ArrayList<String> cuoti=new ArrayList<String>();
public static void main(String[] str) {
ArrayList<String> v=new ArrayList<String>();
System.out.println("要打印多少题?");
int s=sc.nextInt();
String kong=sc.nextLine();
System.out.println("是否应包含*/法?(1.包含/2.不包含)");
int m=sc.nextInt();
kong=sc.nextLine();
System.out.println("每个式子应有几个数");
int n=sc.nextInt();

kong=sc.nextLine();
System.out.println("有无括号(若操作数小于等于2则不需要)(1.要/2.不要)");
int f=sc.nextInt();
kong=sc.nextLine();
System.out.println("输入操作数范围");
int fhm=sc.nextInt();
kong=sc.nextLine();
shengchengshizi(s,m,n,f,fhm,v);
cuotichongzuo();
}
static void cuotichongzuo() {
int c=0;
aa1++;
for(int i=0;i<cuoti.size();i++) {
System.out.print(cuoti.get(i)+"=");
if(!(dati(cuoti.get(i)))) {
System.out.println("答错了");
c++;
}
if(aa1==5)
c=0;
if(c!=0)
cuotichongzuo();
}
}
static boolean chachong(ArrayList<String> x,String z){
boolean s=true;
for(int i=0;i<x.size();i++) {
if(z.equals(x.get(i))){
s=false;
}
}
return s;
}
static boolean youwukuohao(String x) {
boolean b=false;
for(int i=0;i<x.length();i++)
if(x.charAt(i)==40) {
b=true;
break;
}
return b;
}
static boolean dati(String x) {
boolean xxx;
String y=x,z=x;
if(youwukuohao(x)) {
double jjj=0;
if(x.charAt(0)!=‘(‘&&x.charAt(x.length()-1)!=‘)‘) {
String[] n11=x.split("[(]");
String[] n22=n11[1].split("[)]");
switch(n22[0].charAt(1)) {
case ‘+‘:jjj=Integer.parseInt(""+n22[0].charAt(0))+Integer.parseInt(""+n22[0].charAt(2));break;
case ‘-‘:jjj=Integer.parseInt(""+n22[0].charAt(0))-Integer.parseInt(""+n22[0].charAt(2));break;
case ‘*‘:jjj=Double.parseDouble(""+n22[0].charAt(0))*Double.parseDouble(""+n22[0].charAt(2));break;
case ‘/‘:jjj=Double.parseDouble(""+n22[0].charAt(0))/Double.parseDouble(""+n22[0].charAt(2));break;
}
y=n11[0]+jjj+n22[1];
}else if(x.charAt(0)==‘(‘){
String[] n11=x.split("[(]");
String[] n22=n11[1].split("[)]");
switch(n22[0].charAt(1)) {
case ‘+‘:jjj=Integer.parseInt(""+n22[0].charAt(0))+Integer.parseInt(""+n22[0].charAt(2));break;
case ‘-‘:jjj=Integer.parseInt(""+n22[0].charAt(0))-Integer.parseInt(""+n22[0].charAt(2));break;
case ‘*‘:jjj=Double.parseDouble(""+n22[0].charAt(0))*Double.parseDouble(""+n22[0].charAt(2));break;
case ‘/‘:jjj=Double.parseDouble(""+n22[0].charAt(0))/Double.parseDouble(""+n22[0].charAt(2));break;
}
y=jjj+n22[1];
}else if(x.charAt(x.length()-1)==‘)‘){
String[] n11=x.split("[(]");
String[] n22=n11[1].split("[)]");
switch(n22[0].charAt(1)) {
case ‘+‘:jjj=Integer.parseInt(""+n22[0].charAt(0))+Integer.parseInt(""+n22[0].charAt(2));break;
case ‘-‘:jjj=Integer.parseInt(""+n22[0].charAt(0))-Integer.parseInt(""+n22[0].charAt(2));break;
case ‘*‘:jjj=Double.parseDouble(""+n22[0].charAt(0))*Double.parseDouble(""+n22[0].charAt(2));break;
case ‘/‘:jjj=Double.parseDouble(""+n22[0].charAt(0))/Double.parseDouble(""+n22[0].charAt(2));break;
}
y=n11[0]+jjj;
}
}
double ih=0;
String[] x3=y.split("[+]");
for(int k=0;k<x3.length;k++) {
String[] x4=x3[k].split("-");
for(int l=0;l<x4.length;l++) {
String[] x5=x4[l].split("[*]");
for(int m=0;m<x5.length;m++) {
String[] x6=x5[m].split("[/]");
double nm=Double.parseDouble(x6[0]);
for(int n=1;n<x6.length;n++)
{
nm/=Double.parseDouble(x6[n]);
}
x5[m]=""+nm;
}
double ml=1;
for(int n=0;n<x5.length;n++)
ml*=Double.parseDouble(x5[n]);
x4[l]=""+ml;
}
double lk=Double.parseDouble(x4[0]);
for(int n=1;n<x4.length;n++)
lk-=Double.parseDouble(x4[n]);
x3[k]=""+lk;
}
double kj=0;
for(int n=0;n<x3.length;n++)
kj+=Double.parseDouble(x3[n]);
ih=+kj;
System.out.print(ih+" ");
double vv=sc.nextDouble();
if(Math.abs(vv-ih)<0.1)
xxx=true;
else
xxx=false;
return xxx;
}
static void shengchengshizi(int tiliang,int chengchu,int shuzigeshu,int youwukuohao,int caozuofanwei,ArrayList<String> z) {
int b=0,d=0;
double c=0;
int sd = 0,md=0;
String yunsuanfu="+-*/";
Random a=new Random();
for(int i=0;i<tiliang;i++) {
String x="";
if(youwukuohao==1)
{
sd=a.nextInt()%2;
}
int sf=1,jb=0;
for(int j=0;j<shuzigeshu;j++)
{
b=a.nextInt(caozuofanwei)+1;
if(chengchu==1)
d=a.nextInt(4);
else
d=a.nextInt(2);
if(a.nextInt()%2==0&&jb==0&&j<shuzigeshu-1)
{
if(youwukuohao==1)
{
if(sd==1&&sf==1)
{
x+="(";
sf++;
}
}
jb++;
}
x+=b;
if(youwukuohao==1&&sf==2)
{
if(sd==1)
if(md%2==1&&md>0)
{
x+=")";
sf=0;
md=0;
}
}
if(j!=shuzigeshu-1)
x+=yunsuanfu.charAt(d);
if(sf==2)
md++;
if(j==shuzigeshu-1)
jb=0;
}
if(chachong(z,x))
{
System.out.print(x+"=");
if(!(dati(x))) {
System.out.println("答错了");
cuoti.add(x);
c++;
}
z.add(x);
}
else
i--;
}
System.out.println("错误率:"+(double)c/tiliang*100+"%");
}
}

随机生成四则运算并答题

原文:https://www.cnblogs.com/duanzheng/p/15335746.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!