四则运算第二阶段

package pk1;
import java.util.Random;
import java.util.Scanner;
public class sizeyunxuan {
    

    public static void main(String[] args) {
        Scanner scan =new Scanner(System.in);
        int n,num,sum=0;
        System.out.println("请输入需要的题目数:");
        n=scan.nextInt();
        System.out.println("请输入需要的操作数(操作数需要大于等于2):");
        num=scan.nextInt();
        int i,j;
        if (num==2) {//两个操作数
            for(i =0;i<n;i++) {
            
            int a=ShuJu.shuju1();
int b=ShuJu.shuju2(); shencha shencha=new shencha(); int flag; flag=shencha.arraySortedIsEqual1(a, b); if (flag==1) a++; String fu=ShuJu.yunsuan(); System.out.print(a+fu+b+"="); int c=OperateAnswer.Answer(a,fu,b); int s; s=scan.nextInt(); int flag1=judgeTF.judge(c, s); System.out.println("正确答案为:"+s); if(flag1==1){ sum++; } System.out.println("目前得分为:"+sum); System.out.println(" "); } int p=n-sum; System.out.println("总得分为:"+sum+",错误率为:"+(double)(p)/n); } else { for(i =0;i<n;i++) {//三个及以上操作数 int a=ShuJu.shuju1(); int b=ShuJu.shuju2(); shencha shencha=new shencha(); int flag; flag=shencha.arraySortedIsEqual1(a, b); if (flag==1) a++; String fu1=ShuJu.yunsuan(); System.out.print(+a+fu1+b); int m= OperateAnswer.Answer(a,fu1,b); for(j=1;j<num-1;j++) { int c=ShuJu.shuju1(); String fu2=ShuJu.yunsuan(); System.out.print(fu2+c); if((fu2=="+"||fu2=="-")&&(fu1=="*"||fu1=="/")) {m=OperateAnswer.Answer(m,fu2,c);} else if((fu2=="+"||fu2=="-")&&(fu1=="+"||fu1=="-")) {m=OperateAnswer.Answer(m,fu2,c);} else if((fu2=="*"||fu2=="/")&&(fu1=="*"||fu1=="/")) {m=OperateAnswer.Answer(m,fu2,c);} else if((fu2=="*"||fu2=="/")&&(fu1=="+"||fu1=="-")) {int p=OperateAnswer.Answer(b,fu2,c); m=OperateAnswer.Answer(a,fu1,p);} } System.out.println("="); int s; s=scan.nextInt(); int flag1=judgeTF.judge(m, s); System.out.println("正确答案为:"+m); if(flag1==1){ sum++; } System.out.println("目前得分为:"+sum); System.out.println(" "); } int p=n-sum; System.out.println("总得分为:"+sum+",错误率为:"+(double)(p)/n); } } }

 

计算部分

package pk1;

public class OperateAnswer {


public static int Answer(int a,String fu,int b) {
    
    int ans = 0;
    if(fu=="+")
    {
        ans=a+b;
    }
    else if(fu=="-") {
        ans=a-b;
    }
    else if(fu=="*") {
        ans=a*b;
    }
    else if(fu=="/"){
        ans=a/b;
    }
    return ans;
}
}

 

判断是否重复部分

package pk1;
public class shencha {

        }
    public int arraySortedIsEqual1(int a, int b) {
        if (a==b)
            return 1;
        else
        return 0;
    }

    
}
     
    

判断是否正确部分

package pk1;

public class judgeTF {
    public static int judge( int ans,int s) {
        int flag=0;

        if(ans==s) {
            flag=1;
            System.out.println("计算正确!");
        }
        else if(ans!=s) {    
            flag=0;
            System.out.println("计算错误!");
        }
        return flag;
    }

}

数据部分

package pk1;
import java.util.Random;

public class ShuJu {
    
   private int shuju1;//数据1
   private int shuju2;//数据2
   private String fuhao;//符号
   
  public static int shuju1() {
       Random r= new Random();
        int shuju1=r.nextInt(99);
        return shuju1;
    }
  
  public static int shuju2() {
       Random r= new Random();
        int shuju2=r.nextInt(99);
        return shuju2;
    }
 
  public static String yunsuan( ) {
        Random r=new Random();
        int t=(int)r.nextInt(4);
        String fuhao= " ";
        switch(t) {
        case 0:
            fuhao = "+";
            break;
        case 1:
                fuhao = "-";
                break;
        case 2:
                fuhao = "*";
                break;
         case 3:
                fuhao = "/";
                break;
        }
        return fuhao;
  }
 
    ShuJu(int shuju1,int shuju2,String fuhao){
        this.shuju1=shuju1;
        this.shuju2=shuju2;
        this.fuhao=fuhao;
    }
    

   
 }

 

posted @ 2021-09-26 21:38  zrswheart  阅读(50)  评论(0编辑  收藏  举报