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+" = ");
		}
	}
}

  代码运行结果:

转载于:https://www.cnblogs.com/zchenjian/p/4319076.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值