zoukankan      html  css  js  c++  java
  • 2020.9.28

    一、今日学习内容:、

    今天研究的一个算法是随机生成三十道一百以内的加减法另外还有九九乘法表内的乘除法:

    //20194021 xin1905-2 marongrong
    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;
                     }
                /* if(sign == '×')
                 {
                     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++;
                 }*/
                /*
                 * 加减法判断*/     
                /* if(sign == '+' || sign == '-')
                 {
                     if(x < y)                        //判断减数与被减数的大小关系
                     {
                         int temp;
                         temp = x;
                         x = y;
                         y = temp;
                     }
                     System.out.println( "("+ (i+1) +")"+ x + " " + sign + " " + y + "="     );
                     i++;
                 }*/
                 
                 /*
                  * 除法判断*/
                /* if(sign == '÷')
                 {
                    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++;
    }
    */ } while(i<30); }
  • 相关阅读:
    第一阶段用户模板和用户场景
    团队开发冲刺第十二天
    第十周总结
    团队开发冲刺第十一天
    团队开发冲刺第十天(实现页面展示评论数与点赞数)
    团队开发冲刺第九天(实现收藏,点赞,关注功能)
    团队开发冲刺第八天(实现评论功能)
    软件用户场景分析
    第九周总结
    团队开发冲刺第六天(新闻详情页的展示)
  • 原文地址:https://www.cnblogs.com/marr/p/14169916.html
Copyright © 2011-2022 走看看