Generate random static final number in java

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

im making a game for mi school and i cant do that a static final int gets a random number, the game is asteroids and the asteroids are make with polygons, i can change the points of the polygon with this:

private static final int NUMBER_OF_POINTS = 5;

and if i put

private static final int NUMBER_OF_POINTS = 3;

it makes a triangle but i want that when the game start it generate random figures this by generating random numbers in the :

private static final int NUMBER_OF_POINTS = 3;

here is what i have made, but it doesnt work:

private static final int NUMBER_OF_POINTS = 3 + (int)(Math.random() * (5 - 3) + 1));

and here is the hole code :

/*
 * 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);
    }

}

Answers

try this instead

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.

i have a feeling you cant change the value of a final variable so maybe get rid of final. Let me know if this is along the line of what you were looking for.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/40795765/generate-random-static-final-number-in-java

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils