JAVA-随机生成四则运算

代码如下:

  1 package jiajianchengchu;
  2 import java.util.*;
  3 public class jisuan {
  4 public static String yuefen(int a,int b){
  5         int y = 1;
  6         for(int i=a;i>=1;i--){
  7             if(a%i==0&&b%i==0){
  8                 y = i;
  9                 break;
 10             }
 11         }
 12         int z = a/y;
 13         int m = b/y;
 14         if(z==0) {
 15             return "0";
 16         }
 17         return ""+z+"/"+m;
 18     }
 19     public static void main(String[] args) {
 20         // TODO Auto-generated method stub
 21         int x,y,i,a1;
 22         int d1=0;
 23         String d = null;
 24         System.out.println("1、整数计算式,2、真分数计算式");
 25         Scanner scan1=new Scanner(System.in);
 26         a1=scan1.nextInt();
 27 if(a1==1){
 28         System.out.println("请输入题目的数量");
 29         Scanner scan2=new Scanner(System.in);
 30         x=scan2.nextInt();
 31         int daan[]=new int [x];
 32         System.out.println("请输入数值的范围");
 33         y=scan2.nextInt();
 34         for(i=0;i<x;i++){
 35             int a=(int)(Math.random()*y);//随机生成一个1-10的整数
 36             int b=(int)(Math.random()*y);//随机生成一个1-10的整数
 37             int c=(int)(Math.random()*3);//随机生成一个1-4的整数,0表示加法,1表示减法,2表示乘法,3表示除法
 38             if(c==0)
 39             {
 40                 d1=a+b;
 41                 System.out.print(a+"+"+b+"=       ");
 42             }
 43             if(c==1)
 44             {
 45                 d1=a-b;
 46                 System.out.print(a+"-"+b+"=       ");
 47             }
 48             if(c==2)
 49             {
 50                 d1=a*b;
 51                 System.out.print(a+"*"+b+"=       ");
 52             }
 53             if(c==3)
 54             {
 55                 d1=a/b;
 56                 System.out.print(a+"/"+b+"=       ");
 57             }
 58             if((i+1)%3==0){
 59                 System.out.println();
 60             }
 61             daan[i]=d1;
 62         }
 63         System.out.println("是否显示答案(显示请输入1)");
 64         if(scan2.nextInt()==1){
 65             for(i=0;i<x;i++){
 66             System.out.print(daan[i]+"     ");
 67             }
 68         } 
 69         }
 70 if(a1==2){
 71             int M,Z;
 72             System.out.println("请输入题目的数量");
 73             Scanner scan2=new Scanner(System.in);
 74             x=scan2.nextInt();
 75             String daan[]=new String [x];
 76             int x1,x2,B,m1,m2;
 77             System.out.println("请输入分母数值的范围");
 78             B=scan2.nextInt();
 79             for(i=0;i<x;i++){
 80                 m1=1+(int)(Math.random()*B);//随机生成一个小于B的分母
 81                 x1=1+(int)(Math.random()*m1);//生成一个比分母小的分子,实现真分数
 82                 m2=1+(int)(Math.random()*B);//随机生成一个小于B的分母
 83                 x2=1+(int)(Math.random()*m2);//生成一个比分母小的分子,实现真分数
 84                 int c=(int)(Math.random()*3);//生成运算符
 85                 if(c==0){
 86                     Z=x1*m2+x2*m1;
 87                     M=m1*m2;
 88                     d=yuefen(Z,M);
 89                     System.out.print(x1+"/"+m1+"+"+x2+"/"+m2+"=       ");
 90                 }
 91                 if(c==1){
 92                     Z=x1*m2-x2*m1;
 93                     M=m1*m2;
 94                     d=yuefen(Z,M);
 95                     System.out.print(x1+"/"+m1+"-"+x2+"/"+m2+"=       ");
 96                 }
 97                 if(c==2){
 98                     Z=x1*x2;
 99                     M=m1*m2;
100                     d=d=yuefen(Z,M);
101                     System.out.print(x1+"/"+m1+"*"+x2+"/"+m2+"=       ");
102                 }
103                 if(c==3){
104                     Z=m1*x2;
105                     M=m2*x1;
106                     d=d=yuefen(Z,M);
107                     System.out.print(x1+"/"+m1+"/"+x2+"/"+m2+"=       ");
108                 }
109                 if((i+1)%3==0){
110                     System.out.println();
111                 }
112                 daan[i]=d;
113                 
114             }
115             System.out.println("是否显示答案(显示请输入1)");
116             if(scan2.nextInt()==1){
117                 for(i=0;i<x;i++){
118                 System.out.print(daan[i]+"     ");
119                 }
120             }
121             
122         }
123 
124         
125     }
126 
127     }

 

转载于:https://www.cnblogs.com/ziyixuedie/p/6492163.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值