Java 如何生成2个随机整数,介于1和7之间,而两者都不是5?
编辑:这是一个不同的问题,因为我要求解决方案使用的方法Java 如何生成2个随机整数,介于1和7之间,而两者都不是5?,java,random,int,greenfoot,Java,Random,Int,Greenfoot,编辑:这是一个不同的问题,因为我要求解决方案使用的方法 public int getRandomNumber(int start,int end) { int normal = Greenfoot.getRandomNumber(end-start+1); return normal+start; } 及 下午好。我一直在用Java在Greenfoot做一个项目,主角在一个7x7屏幕上,有3枚硬币。我编写了以下代码: public class FirstLevel extends World
public int getRandomNumber(int start,int end)
{
int normal = Greenfoot.getRandomNumber(end-start+1);
return normal+start;
}
及
下午好。我一直在用Java在Greenfoot做一个项目,主角在一个7x7屏幕上,有3枚硬币。我编写了以下代码:
public class FirstLevel extends World
{
public int getRandomNumber(int start,int end)
{
int normal = Greenfoot.getRandomNumber(end-start+1);
return normal+start;
}
/**
* Constructor for objects of class FirstLevel.
*
*/
public FirstLevel()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(9, 9, 60);
MainCharacter theDuckDude = new MainCharacter ();
addObject(theDuckDude, 4, 4);
coin coin1 = new coin();
coin coin2 = new coin();
coin coin3 = new coin();
addObject(coin1, getRandomNumber(1, 7), getRandomNumber(1, 7));
addObject(coin2, getRandomNumber(1, 7), getRandomNumber(1, 7));
addObject(coin3, getRandomNumber(1, 7), getRandomNumber(1, 7));
}
}
因此,正如您所看到的,三枚硬币和鸭子角色将随机出现在屏幕上。以下是我的dukdude字符的代码片段:
Actor actor = getOneIntersectingObject(coin.class);
getWorld().removeObject(actor);
显然,这段代码显示,当我的theDuckDude角色接触硬币角色时,硬币被移除。正如您可能已经看到的,这带来了一个相当大的问题:如果dukdude和coin actor在同一个正方形上生成,游戏将无法正常运行:
那么,有没有一种方法可以使用
public int getRandomNumber(int start,int end)
{
int normal = Greenfoot.getRandomNumber(end-start+1);
return normal+start;
}
及
方法,如何使其生成一个随机位置,将5和5一起排除在外?有49个可见正方形,但有48个有效正方形。从0到47(含0到47)之间选择一个整数,加1以排除中间的平方,然后从中导出
x
和y
static final int ROWS = 7;
static final int COLS = 7;
static final int EXCLUDE_X = 5;
static final int EXCLUDE_Y = 5;
int index = ThreadLocalRandom.current().nextInt(ROWS * COLS - 1); // range: [0, 47]
if (index >= ((EXCLUDE_Y * ROWS) + EXCLUDE_X)) { // offset the center square
index++;
}
int x = (index % COLS);
int y = (index / ROWS);
我在这里坚持使用ThreadLocalRandom,但是你可以很容易地改写同样的内容来与你的绿脚助手一起工作。实际上,基本上有两种方法 简单的方法是,选择两个随机整数。如果你击中了duckDude的位置,你会再次选择。直到你得到一个不同的位置:
int x;
int y;
do {
x = getRandomNumber(1, 7);
y = getRandomNumber(1, 7);
} while (x == duckDudeX && y == duckDudeY);
addObject(character, x, y);
它起作用了。但令人不满意的是,即使它最终会终止,我们也不知道迭代次数的上限
另一个选项是在拾取方形时已排除禁止的方形。这也是其他几个答案的基本思想。我的版本使用了有效的方坐标列表,因此需要进行一些初始化:
List<Pair> validPairs = new ArrayList<>(49);
for (int x = 1; x <= 7; x++) {
for (int y = 1; y <= 7; y++) {
validPairs.add(new Pair(x, y));
}
}
validPairs.remove(new Pair(duckDudeX, duckDudeY));
辅助Pair
类为项目添加了一些行,不过:
public class Pair {
private int x;
private int y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Pair other = (Pair) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
}
编辑:我希望你能自己填写缺失的部分,如果有任何与你的代码不一致的地方也能改正。在我的代码中,我有:
static final int duckDudeX = 4;
static final int duckDudeY = 4;
从你的问题和代码中不清楚(4,4)或(5,5)是禁地。当然,只要我的代码显示
duckDudeX
或duckDudeY
,您就可以填写4或5。如果是我的代码,我会坚持声明变量或常量。你最清楚常数是否合适,也可能找到更好的名称。如果我理解正确,位置5,5只是一个例子,你正在寻找一种在给定范围内生成不同随机数的算法
通常,您会预先洗牌索引数组,然后逐个检索洗牌后的索引。但是,如果您想在两者之间手动选取索引,这将不起作用(例如:生成随机硬币->移除特定硬币->生成另一个随机硬币)
此问题的解决方案是实施Fisher-Yates shuffle算法的替代版本:
static void shuffle(int[] ar) {
Random rnd = ThreadLocalRandom.current();
for (int i = ar.length - 1; i > 0; i--){
int index = rnd.nextInt(i + 1);
int a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
}
正如您所看到的,一旦一个数组项被洗牌到数组的末尾,它将不再改变其位置。因此,我们可以移除循环并逐个检索被洗牌的项或索引,而不是得到相同的结果:
public class Shuffler {
private final int indexCount;
private final int[] indexes;
private final int[] indexIndexes;
private int nextIndexI;
public Shuffler(int indexCount){
this.indexCount = indexCount;
indexes = new int[indexCount];
indexIndexes = new int[indexCount];
for(int i = 0; i < indexCount; i++){
indexes[i] = i;
indexIndexes[i] = i;
}
nextIndexI = indexCount - 1;
}
public int nextIndex(){
if(nextIndexI == -1){
return -1;
}
Random rnd = ThreadLocalRandom.current();
int i = rnd.nextInt(nextIndexI + 1);
swap(i, nextIndexI);
return indexes[nextIndexI--];
}
public boolean pickIndex(int index){
if(0 > index || index >= indexCount || indexIndexes[index] > nextIndexI){
return false;
}
swap(indexIndexes[index], nextIndexI);
nextIndexI--;
return true;
}
public boolean reinsertIndex(int index){
if(0 > index || index >= indexCount || indexIndexes[index] <= nextIndexI){
return false;
}
nextIndexI++;
swap(indexIndexes[index], nextIndexI);
return true;
}
private void swap(int i1, int i2){
indexIndexes[indexes[i1]] = i2;
indexIndexes[indexes[i2]] = i1;
int tmp = indexes[i1];
indexes[i1] = indexes[i2];
indexes[i2] = tmp;
}
}
当然,您可以替换以下行:
Random rnd = ThreadLocalRandom.current();
int i = rnd.nextInt(nextIndexI + 1);
使用您自己的
getRandomNumber()
方法。这个问题的答案很简单,但要小心!,你错过了一些东西。请遵守下面的代码
public class FirstLevel extends World
{
public int getRandomNumber(int start,int end)
{
int normal = Greenfoot.getRandomNumber(end-start+1);
return normal+start;
}
public FirstLevel()
{
super(9, 9, 60);
int coin1x = 0;
int coin2x = 0;
int coin3x = 0;
int coin1y = 0;
int coin2y = 0;
int coin3y = 0;
MainCharacter theDuckDude = new MainCharacter ();
addObject(theDuckDude, 4, 4);
coin coin1 = new coin();
coin coin2 = new coin();
coin coin3 = new coin();
coin1x = getRandomNumber(1, 9);
coin1y = getRandomNumber(1, 9);
coin2x = getRandomNumber(1, 9);
coin2y = getRandomNumber(1, 9);
coin3x = getRandomNumber(1, 9);
coin3y = getRandomNumber(1, 9);
while (coin1x == 4 && coin1y == 4)
{
coin1x = getRandomNumber(1, 9);
coin1y = getRandomNumber(1, 9);
}
while (coin2x == 4 && coin2y == 4)
{
coin2x = getRandomNumber(1, 9);
coin2y = getRandomNumber(1, 9);
while (coin2y == coin1y && coin2x == coin1x);
{ coin2x = getRandomNumber(1, 9);
coin2y = getRandomNumber(1, 9);
}
while (coin2y == coin3y && coin2x == coin3x);
{
coin2x = getRandomNumber(1, 9);
coin2y = getRandomNumber(1, 9);
}
while (coin1x == coin3x && coin1y == coin3y);
{
coin3x = getRandomNumber(1, 9);
coin3y = getRandomNumber(1, 0);
}
}
while (coin3x == 4 && coin3y == 4)
{
coin3x = getRandomNumber(1, 9);
coin3y = getRandomNumber(1, 9);
}
int x;
int y;
addObject(coin1, coin1x, coin1y);
addObject(coin2, coin2x, coin2y);
addObject(coin3, coin3x, coin3y);
请注意,这段代码有两个您遗漏的新内容。首先,此代码将检查硬币是否放置在4,4正方形上:
while (coin1x == 4 && coin1y == 4)
{
coin1x = getRandomNumber(1, 9);
coin1y = getRandomNumber(1, 9);
}
但也要注意,代码将检查两枚硬币是否在同一个正方形上:
while (coin2y == coin1y && coin2x == coin1x);
{ coin2x = getRandomNumber(1, 9);
coin2y = getRandomNumber(1, 9);
}
你有没有想过测试它们是相同的,如果它们是另一对的?重新打开,因为这是一个2D问题,并且没有统计上的明显。硬币能放在同一个方块上吗?我会做一个循环,1,生成点。2、检查是否有人。3,如果否,则为环路外。否则,请重新生成。@马特:是的,我正在试图弄清楚。错误:没有找到DuckDudex变量。请自己初始化它们,并在为硬币生成随机坐标之前使用duckdude的x和y坐标对它们进行更新。对不起,禁止使用的正方形是4,4
Random rnd = ThreadLocalRandom.current();
int i = rnd.nextInt(nextIndexI + 1);
public class FirstLevel extends World
{
public int getRandomNumber(int start,int end)
{
int normal = Greenfoot.getRandomNumber(end-start+1);
return normal+start;
}
public FirstLevel()
{
super(9, 9, 60);
int coin1x = 0;
int coin2x = 0;
int coin3x = 0;
int coin1y = 0;
int coin2y = 0;
int coin3y = 0;
MainCharacter theDuckDude = new MainCharacter ();
addObject(theDuckDude, 4, 4);
coin coin1 = new coin();
coin coin2 = new coin();
coin coin3 = new coin();
coin1x = getRandomNumber(1, 9);
coin1y = getRandomNumber(1, 9);
coin2x = getRandomNumber(1, 9);
coin2y = getRandomNumber(1, 9);
coin3x = getRandomNumber(1, 9);
coin3y = getRandomNumber(1, 9);
while (coin1x == 4 && coin1y == 4)
{
coin1x = getRandomNumber(1, 9);
coin1y = getRandomNumber(1, 9);
}
while (coin2x == 4 && coin2y == 4)
{
coin2x = getRandomNumber(1, 9);
coin2y = getRandomNumber(1, 9);
while (coin2y == coin1y && coin2x == coin1x);
{ coin2x = getRandomNumber(1, 9);
coin2y = getRandomNumber(1, 9);
}
while (coin2y == coin3y && coin2x == coin3x);
{
coin2x = getRandomNumber(1, 9);
coin2y = getRandomNumber(1, 9);
}
while (coin1x == coin3x && coin1y == coin3y);
{
coin3x = getRandomNumber(1, 9);
coin3y = getRandomNumber(1, 0);
}
}
while (coin3x == 4 && coin3y == 4)
{
coin3x = getRandomNumber(1, 9);
coin3y = getRandomNumber(1, 9);
}
int x;
int y;
addObject(coin1, coin1x, coin1y);
addObject(coin2, coin2x, coin2y);
addObject(coin3, coin3x, coin3y);
while (coin1x == 4 && coin1y == 4)
{
coin1x = getRandomNumber(1, 9);
coin1y = getRandomNumber(1, 9);
}
while (coin2y == coin1y && coin2x == coin1x);
{ coin2x = getRandomNumber(1, 9);
coin2y = getRandomNumber(1, 9);
}