zoukankan      html  css  js  c++  java
  • 四则运算

    课堂测试

    1、题目要求

     一家软件公司程序员二柱的小孩上了小学二年级,老师让家长每天出30道四则运算题目给小学生做。

    2、思路

    利用java的随机函数产生两个随机数和一个随机运算符(分别用数字1,2,3,4表示+,-,*,/四个运算符),输入想要产生的题数和每行显示的题数,在输出随机数和随机运算符前判断(乘法的结果小于100,如果不符合,跳过本次循环;除法余数不能为0,如果为0,跳过本次循环)

    3、源代码

    import java.util.Scanner;
    public class Calculate {
     public static void main(String[] args) {
         Scanner scan=new Scanner(System.in);
         int n;
         int p;
         System.out.println("please input the number of the exercises");
         n=scan.nextInt();
         System.out.println("please input how many exercises are showed in one line");
         p=scan.nextInt();
         for(int i=1;i<=n;i++)
         {
            int x=(int)(Math.random()*100);
         int y=(int)(Math.random()*100+1);
         int operator=(int)(Math.random()*4+1);
         if(operator==2)
         {
            if(x<y)
            {
               int temp;
               temp=x;
               x=y;
               y=temp;
            }
         }
         if(operator==3)
         {
            if(x*y>=100)
            {
               i--;  
                 continue;
            }
         }
         if(operator==4)
         {
          if(x%y!=0)
          {
             i--;
             continue;
          }
        }
       System.out.print(i+"、");
       System.out.print(x);
       if(operator==1)
       {
          System.out.print("+");
       }
       else if(operator==2)
       {
          System.out.print("-");
       }
       else if(operator==3)
       {
          System.out.print("*");
       }
       else if(operator==4)
       {
          System.out.print("/");
       }
       System.out.print(y+"  ");
       if(i%p==0)
        System.out.println();
      }
     }
    }

    4、结果

    please input the number of the exercises
    100
    please input how many exercises are showed in one line
    5
    1、10-2  2、90-36  3、48+15  4、99+9  5、92-12 
    6、25+100  7、36-30  8、62+62  9、8+65  10、65+36 
    11、73-48  12、13+13  13、93-40  14、56+24  15、85+19 
    16、86+98  17、40-8  18、21+75  19、96-74  20、66+67 
    21、81+99  22、66+22  23、20-16  24、99+27  25、63+77 
    26、94+24  27、78+76  28、3*24  29、57-9  30、41+10 
    31、60+92  32、51-44  33、12+40  34、49-29  35、65-29 
    36、62+27  37、21+56  38、32+6  39、40/10  40、14+48 
    41、97-84  42、48+79  43、91-42  44、77-22  45、33+73 
    46、24-23  47、99+78  48、81-50  49、2+86  50、88+43 
    51、89-52  52、68+71  53、38+47  54、94+22  55、18+40 
    56、60-52  57、58+41  58、52+45  59、63-57  60、61+63 
    61、67-23  62、79+2  63、6+28  64、22+48  65、11-0 
    66、4+97  67、99-24  68、46/1  69、69+4  70、49-5 
    71、87-74  72、93-35  73、74-40  74、66+3  75、89-39 
    76、46+56  77、71+47  78、89-47  79、63-30  80、55+55 
    81、86+8  82、1+60  83、91+22  84、74+97  85、19/1 
    86、9+74  87、89-8  88、66-9  89、10+87  90、45-8 
    91、71+71  92、44+83  93、7+44  94、15+82  95、67-35 
    96、27-18  97、53+24  98、99-35  99、20-5  100、72+12 

  • 相关阅读:
    HTTP协议
    安全测试-渗透性测试
    网络安全、Web安全、渗透测试之笔经面经总结(三)
    Struts2拦截器再认识
    struts2.5+框架使用通配符与动态方法
    struts.xml配置详解
    代理概述
    Mybatis Plugin(拦截器)的开发
    详解环境搭建SSM
    mybatis-databaseIdProvider多数据库支持
  • 原文地址:https://www.cnblogs.com/songxinai/p/11580570.html
Copyright © 2011-2022 走看看