在java中生成随机静态最终数字

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在java中生成随机静态最终数字相关的知识,希望对你有一定的参考价值。

我正在为我的学校制作一个游戏,我不能这样做一个静态的最终int获得一个随机数,游戏是小行星,小行星是用多边形制作的,我可以用这样改变多边形的点:

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.

我有一种感觉,你不能改变最终变量的价值,所以也许摆脱最终。如果这与您所寻找的一致,请告诉我。

以上是关于在java中生成随机静态最终数字的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Java 中生成随机排列?

如何在java中生成一个随机的15位长[重复]

在 Ms. Access 中生成随机字母数字密钥

在java中生成随机字符串[重复]

python随机彩票号码生成器游戏

如何在java中生成6个不同的随机数