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

    题目:

    V1.0花二十分钟写一个能自动生成30道小学四则运算题目的 “软件”

    V2.0题目避免重复;可定制(数量/打印方式);

    package Sizeyunsuan;
    /**
     * 30道100以内四则运算
     * 
     * */
    public class Random {
    
        public static int getRandom( int n, int m)
             {
            //产生n->m的随机数
                    return (int) (Math.random() * (m - n) + n);
             }
        
        public static char getCharRandom()
        {
            //随机产生四种运算符
            char sign = 0;
            int Sn;
            Sn = getRandom(1,5);
            switch(Sn)
            {
                case 1:sign = '+' ;break;
                case 2:sign = '-' ;break;
                case 3:sign = '×' ;break;
                case 4:sign = '÷' ;break;
            }
            return sign;
        }
        
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
             int i = 0;
             do
             {
                 int  x = (int) (Math.random() * (100 - 1 )+ 1);  //产生1-100的随机数
                 int  y = (int) (Math.random() * (100 - 1 )+ 1);  //产生1-100的随机数
                 char sign = getCharRandom();
                 /*
                  * 判断乘法的范围*/
                 switch(sign)
                 {
                 case '+':
                     System.out.println( "("+ (i+1) +")"+ x + " " + sign + " " + y + "="     );
                     i++;break;
                 case '-':
                     if(x < y)                        //判断减数与被减数的大小关系
                     {
                         int temp;
                         temp = x;
                         x = y;
                         y = temp;
                     }
                     System.out.println( "("+ (i+1) +")"+ x + " " + sign + " " + y + "="     );
                     i++;break;
                 case '×':
                 {
                     x = (int) (Math.random() * (10 - 1 )+ 1);//新生成x,y<9的随机数
                     y = (int) (Math.random() * (10 - 1 )+ 1);
                     
                         System.out.println( "("+ (i+1) +")"+ x + " " + sign + " " + y + "="     );    
                         i++;
                 };break;
                 case '÷':
                     do                                             //循环生成除法
                     {
                         y = (int) (Math.random() * (10 - 1 )+ 1);
                         x = (int) (Math.random() * (9*y - 1 )+ 1);
                                                
                     }
                    while(x % y != 0) ;
                    System.out.println( "("+ (i+1) +")"+ x + " " + sign + " " + y + "="     );
                    i++;break;
                     }
            }
            while(i<30);
        }
    
    }

    完善后大概就是这样了,还有功能未完全实现

  • 相关阅读:
    leetcode 437. 路径总和 III
    leetcode 113. 路径总和 II
    题解 【CF387B】George and Round
    题解 【CF489B】 BerSU Ball
    题解【2.23考试T3】val
    题解【2.23考试T1】div
    题解 【洛谷P4290】 [HAOI2008]玩具取名
    题解 【洛谷P1035】[NOIP2002]级数求和
    题解【洛谷P1046】[NOIP2005] 陶陶摘苹果
    题解【洛谷P1618】 三连击(升级版)
  • 原文地址:https://www.cnblogs.com/zyljal/p/14152536.html
Copyright © 2011-2022 走看看