在java中生成随机静态最终数
我正在为我的学校做一个游戏,我不能让一个静态的最终整数得到一个随机数,游戏是小行星,小行星是用多边形制作的,我可以用这个来改变多边形的点:在java中生成随机静态最终数,java,random,numbers,Java,Random,Numbers,我正在为我的学校做一个游戏,我不能让一个静态的最终整数得到一个随机数,游戏是小行星,小行星是用多边形制作的,我可以用这个来改变多边形的点: private static final int NUMBER_OF_POINTS = 5; 如果我把 private static final int NUMBER_OF_POINTS = 3; 它形成了一个三角形,但我希望当游戏开始时,它会通过在下列区域中生成随机数来生成随机数字: private static final int NUMBER_OF
private static final int NUMBER_OF_POINTS = 5;
如果我把
private static final int NUMBER_OF_POINTS = 3;
它形成了一个三角形,但我希望当游戏开始时,它会通过在下列区域中生成随机数来生成随机数字:
private static final int NUMBER_OF_POINTS = 3;
以下是我制作的,但不起作用:
private static final int NUMBER_OF_POINTS = 3 + (int)(Math.random() * (5 - 3) + 1));
下面是全部代码:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package game;
import java.awt.Polygon;
import java.util.Random;
public enum AsteroidSize {
/**
* Small Asteroids have a radius of 15, and are worth 150 points.
*/
XtraSmall(5.0, 150),
/**
* Small Asteroids have a radius of 15, and are worth 100 points.
*/
Small(15.0, 100),
/**
* Medium asteroids have a radius of 25, and are worth 50 points.
*/
Medium(25.0, 50),
/**
* Large asteroids have a radius of 40, and are worth 20 points.
*/
Large(40.0, 20),
/**
* XtraLarge asteroids have a radius of 40, and are worth 10 points.
*/
XtraLarge(50.0, 10);
/**
* The number of points on the Asteroid.
*/
private static final int NUMBER_OF_POINTS = 5;
/**
* The polygon for this type of Asteroid.
*/
public final Polygon polygon;
/**
* The radius of this type of Asteroid.
*/
public final double radius;
/**
* The number of points earned for killing this type of Asteroid.
*/
public final int killValue;
/**
* Creates a new type of Asteroid.
* @param radius The radius.
* @param value The kill value.
*/
private AsteroidSize(double radius, int value) {
this.polygon = generatePolygon(radius);
this.radius = radius + 1.0;
this.killValue = value;
}
/**
* Generates a regular polygon of size radius.
* @param radius The radius of the Polygon.
* @return The generated Polygon.
*/
private static Polygon generatePolygon(double radius) {
//Create an array to store the coordinates.
int[] x = new int[NUMBER_OF_POINTS];
int[] y = new int[NUMBER_OF_POINTS];
//Generate the points in the polygon.
double angle = (2 * Math.PI / NUMBER_OF_POINTS);
for(int i = 0; i < NUMBER_OF_POINTS; i++) {
x[i] = (int) (radius * Math.sin(i * angle));
y[i] = (int) (radius * Math.cos(i * angle));
}
//Create a new polygon from the generated points and return it.
return new Polygon(x, y, NUMBER_OF_POINTS);
}
}
试试这个
Random random = new Random();
int rand;
rand = random.nextInt(8) + 3; // here a number will be generated between 3 and 8, 3 is a triangle 8 octogon so you can change 8 but leave 3 alone.
NUMBER_OF_POINTS = rand; // number of points is now equal to a number between 3 and 8.
我有一种感觉,你不能改变一个最终变量的值,所以也许可以去掉最终变量。让我知道这是否符合您的要求。Define不起作用。你预计会发生什么,会发生什么?我预计生成的小行星有不同的形状,比如如果我放3个点,我会看到一个三角形,如果我放4个点,我会看到一个矩形,相反,它不会显示数字,如果我开始拍摄,我会因为碰撞而得到点,但数字不会出现,所以它与随机静态最终int无关?你在问为什么数字没有出现在屏幕上?嗯,你没有发布任何与在屏幕上显示数字相关的代码,因此很难帮助。你也可以通过改变每边的长度来增加半径。这对我来说是有效的,是的,最终是所有的问题,汤姆我没有想到这一点