zoukankan      html  css  js  c++  java
  • 2位“随机”数四则运算

          今天上课老师现场留了个小作业:产生30个随机数四则运算

    设计思想:

          产生两个随机数,然后运算。再产生一个运算符,判断是加法还是减法。

    问题:

          遇到了些问题,因为初学JAVA,random的用法一直出错。后来网上查了查。才理解。

                Random rand = new Random();

                int firstnum = rand.nextInt(99)+1;可以产生1到99的随机数。如果是从0开始,就是int firstnum = rand.nextInt(100);产生0到99的随机数。

    以下是我的具体代码:

    package jian;
    
    import java.util.Random;
    
    public class jian {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		for(int i=0;i<30;i++){
    			Random rand = new Random();
    			int firstnum = rand.nextInt(99)+1;
    			int secondnum = rand.nextInt(99)+1;
    			int num = rand.nextInt(4)+1;
    			if(num==1)
    				System.out.println(firstnum+" + "+secondnum+" = ");
    			if(num==2)
    				System.out.println(firstnum+" - "+secondnum+" = ");
    			if(num==3)
    				System.out.println(firstnum+" * "+secondnum+" = ");
    			if(num==4)
    				System.out.println(firstnum+" / "+secondnum+" = ");
    		}
    	}
    }
    

      代码运行结果:

  • 相关阅读:
    POJ-2393
    POJ-1328
    POJ-2376
    CF-811B
    CF-811A
    CF-816B
    P1111 修复公路
    P2777 [AHOI2016初中组]自行车比赛
    P1889 士兵站队
    P1459 三值的排序 Sorting a Three-Valued Sequence
  • 原文地址:https://www.cnblogs.com/zchenjian/p/4319076.html
Copyright © 2011-2022 走看看