zoukankan      html  css  js  c++  java
  • 00JAVA语法基础_四则运算 01

    自动生成30道四则运算的数学题,当前只是简单符合出题,答题和判断的代码,还没做要求,所以现在只是能随机生成三十道100以内的加减法和九九乘法表的乘除法

    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);
        }
    
    }

    中间大段注释代码是另一种方法,是第一次写的,之后才换成的switch语句。

    有新的代码,会持续更新……

  • 相关阅读:
    2010,只有感恩。
    用 pythonmessage 为程序库和日志模块解耦
    PHP 范例
    Install Perl CPAN modules
    关于人生的思考
    Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    PHP error: Cannot modify header information headers already sent
    Iceweasel安装Java plugin
    ARP协议以及集线器,交换机,路由器的组合
    Java 字符串操作
  • 原文地址:https://www.cnblogs.com/flw0322/p/9751224.html
Copyright © 2011-2022 走看看