码迷,mamicode.com
首页 > 其他好文 > 详细

随机生成300道四则运算

时间:2017-03-10 00:17:17      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:bsp   size   out   src   ext   ati   and   png   char   

package lianxi;

import java.util.Random;

public class Sizeyunsuan {
 public static void main(String args[]) {
       
        char f[] = { ‘+‘, ‘-‘, ‘*‘, ‘/‘ };
        int max=100;
        for (int i = 0; i < 300; i++) {
            Random random = new Random();
            int a = random.nextInt(max+1);
            int b = random.nextInt(max+1);
            int c = random.nextInt(4);
            int result=-1;
            switch (c) {
            case 0:
                result=a+b;
                break;
            case 1:
                result=a-b;
                break;
            case 2:
                result=a*b;
                break;
            case 3:
                while (true) {
                if (a!=0&&b!=0&&a%b==0) {
                    result=a/b;
                    break;
                }   else {
                     a = random.nextInt(max+1);
                     b = random.nextInt(max+1);
                }         
                }             
                break;
 
            default:
                break;
            }
            System.out.println("" + a + f[c] + b + "="  );
           
           
         
        }
      
    }
 
}技术分享

随机生成300道四则运算

标签:bsp   size   out   src   ext   ati   and   png   char   

原文地址:http://www.cnblogs.com/qjdashuaibi/p/6528383.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!