标签:
今天上课老师现场留了个小作业:产生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+" = "); } } }
标签:
原文地址:http://www.cnblogs.com/zchenjian/p/4319076.html