宾果板随机数问题,java [英] Bingo board random number issues, java

查看:64
本文介绍了宾果板随机数问题,java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以第一个 2列B和我打印随机数很好,Everything打印出相同的东西。我如何解决这个问题



我的尝试:



< pre lang =java> package 赋值;
import java.util.Random;

public class Bingo {

public static void main( String [] args){
Random rand = new Random();
int bLow = 0 ,iLow = 16 ,nLow = 44 ,gLow = 59 ,oLow = 74 ;
int bLimit = 15 ,iLimit = 30 ,nLimit = 45 ,gLimit = 60 ,oLimit = 75 ;
int rB = rand.nextInt(bLimit-bLow)+ bLow,rI = rand.nextInt(iLimit-iLow)+ iLow,rN = rand.nextInt (nLimit-nLow)+ nLow,rG = rand.nextInt(gLimit-gLow)+ gLow,rO = rand.nextInt(oLimit-oLow)+ oLow;
int sB = rand.nextInt(bLimit-bLow)+ bLow,sI = rand.nextInt(iLimit-iLow)+ iLow,sN = rand.nextInt (nLimit-nLow)+ nLow,sG = rand.nextInt(gLimit-gLow)+ gLow,sO = rand.nextInt(oLimit-oLow)+ oLow;
int tB = rand.nextInt(bLimit-bLow)+ bLow,tI = rand.nextInt(iLimit-iLow)+ iLow,tN = rand.nextInt (nLimit-nLow)+ nLow,tG = rand.nextInt(gLimit-gLow)+ gLow,tO = rand.nextInt(oLimit-oLow)+ oLow;
int uB = rand.nextInt(bLimit-bLow)+ bLow,uI = rand.nextInt(iLimit-iLow)+ iLow,uN = rand.nextInt (nLimit-nLow)+ nLow,uG = rand.nextInt(gLimit-gLow)+ gLow,uO = rand.nextInt(oLimit-oLow)+ oLow;
int vB = rand.nextInt(bLimit-bLow)+ bLow,vI = rand.nextInt(iLimit-iLow)+ iLow,vN = rand.nextInt (nLimit-nLow)+ nLow,vG = rand.nextInt(gLimit-gLow)+ gLow,vO = rand.nextInt(oLimit-oLow)+ oLow;
System.out.println( B\t | \tI \ t | \tN \t | \tG\t | \tO);
System.out.println(rB + \t | \t + rI + \t | \t + rN + \t | \t + rG + \t | \t + rO);
System.out.println(sB + \t | \t + sI + \t | \t + sN + \t | \t + sG + \t | \t + sO);
System.out.println(tB + \t | \t + tI + \t | \t + tN + \t | \t + tG + \t | \t + tO);
System.out.println(uB + \t | \t + uI + \t | \t + uN + \t | \t + uG + \t | \t + uO);
System.out.println(vB + \t | \t + vI + \t | \t + vN + \t | \t + vG + \t | \t + vO);

}

}

解决方案

为了获得不同的结果,你有将合理范围传递给 Random.nextInt 方法。



查看(例如)

 rN = rand.nextInt(nLimit-nLow)+ nLow; 

语句。由于 nLimit - nLow = 1 nextInt 方法始终返回 0 并且你总是得到 rN = nLow (另见文档 [ ^ ])。


So the first 2 columns B and I print random numbers fine, Everything after prints the same thing. How do i fix this

What I have tried:

package assignments;
import java.util.Random;

public class Bingo {

	public static void main(String[] args) {
		Random rand = new Random();
		int bLow = 0, iLow = 16, nLow = 44, gLow = 59, oLow = 74;
		int bLimit = 15, iLimit = 30, nLimit = 45, gLimit = 60, oLimit = 75;
		int rB = rand.nextInt(bLimit-bLow) + bLow, rI = rand.nextInt(iLimit-iLow) + iLow, rN = rand.nextInt(nLimit-nLow) + nLow, rG = rand.nextInt(gLimit-gLow) + gLow, rO = rand.nextInt(oLimit-oLow) + oLow;
		int sB = rand.nextInt(bLimit-bLow) + bLow, sI = rand.nextInt(iLimit-iLow) + iLow, sN = rand.nextInt(nLimit-nLow) + nLow, sG = rand.nextInt(gLimit-gLow) + gLow, sO = rand.nextInt(oLimit-oLow) + oLow;
		int tB = rand.nextInt(bLimit-bLow) + bLow, tI = rand.nextInt(iLimit-iLow) + iLow, tN = rand.nextInt(nLimit-nLow) + nLow, tG = rand.nextInt(gLimit-gLow) + gLow, tO = rand.nextInt(oLimit-oLow) + oLow;
		int uB = rand.nextInt(bLimit-bLow) + bLow, uI = rand.nextInt(iLimit-iLow) + iLow, uN = rand.nextInt(nLimit-nLow) + nLow, uG = rand.nextInt(gLimit-gLow) + gLow, uO = rand.nextInt(oLimit-oLow) + oLow;
		int vB = rand.nextInt(bLimit-bLow) + bLow, vI = rand.nextInt(iLimit-iLow) + iLow, vN = rand.nextInt(nLimit-nLow) + nLow, vG = rand.nextInt(gLimit-gLow) + gLow, vO = rand.nextInt(oLimit-oLow) + oLow;
		System.out.println("B\t|\tI\t|\tN\t|\tG\t|\tO");
		System.out.println(rB + "\t|\t" + rI + "\t|\t" + rN + "\t|\t" + rG + "\t|\t" + rO);
		System.out.println(sB + "\t|\t" + sI + "\t|\t" + sN + "\t|\t" + sG + "\t|\t" + sO);
		System.out.println(tB + "\t|\t" + tI + "\t|\t" + tN + "\t|\t" + tG + "\t|\t" + tO);
		System.out.println(uB + "\t|\t" + uI + "\t|\t" + uN + "\t|\t" + uG + "\t|\t" + uO);
		System.out.println(vB + "\t|\t" + vI + "\t|\t" + vN + "\t|\t" + vG + "\t|\t" + vO);

	}

}

解决方案

In order to obtain different results you have to pass sensible ranges to the Random.nextInt method.

Look at (for instance)

rN = rand.nextInt(nLimit-nLow) + nLow;

statement. Since nLimit - nLow = 1, the nextInt method always returns 0 and you always get rN = nLow (see also the documentation[^]).


这篇关于宾果板随机数问题,java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆