Question

Alright I am trying to use the same random function to output diffrent sets of numbers.

Background:

I am developing a plugin for a server and am needing a number generator to make odd's of winning,

What i would like to happen is to pass my odds of winning through a random number generator to then run the code like this,

if ( ten == 0) {
  //ADD STUFF TO PLAYER
return Ten = true;
} else {
  return Ten = false;
}
if (twenty == 0) {
  //ADD STUFF TO PLAYER
  return Twenty = true;
} ECT......

I am using this line of code.

Random rnd = new Random();

int ten = rnd.nextInt(10);
int twenty = rnd.nextInt(20);
ect....

but after the 1'st call to the rnd it gives me syntax errors saying,

Syntax error on token ";", { expected after this token

please help me i have been beating my head against the wall for the last 2 hours!

thank's in advance for your assistance.

EDIT:

public class RewardsExec {


//Registering class as part of the Mojovote Plugin
private Mojovote plugin;
public RewardsExec(Mojovote plugin) {
    this.plugin = plugin;
}

//setting up Random Number Generator
Random rnd = new Random();

ten = rnd.nextInt(10);
twenty = rnd.nextInt(20);
//Odd's Executuion

if (ten = 0) {
    return Ten = true;
} else {
    return Ten = false;
}
    if (twenty == 0 {
            return Twenty = true;
    } else {
            return Twenty = false;
    }
}
Was it helpful?

Solution

As first, there are some errors in your code:

This line produces your syntax error:

if (twenty == 0 {

Change it...

if (twenty == 0) {

Aswell this line doesn't make sense:

if (ten = 0) {

You did a assignment there. Use == or equals() to compare something.

Your Algorithm looks pretty strange. Better use this Method:

public boolean drawNumber(int i) {

    Random rnd = new Random();
    int number = rnd.nextInt(i);

    if (number == 0) {
        return true;
    }

    return false;
}

OTHER TIPS

You need to provide the whole section of code, as somewhere your syntax is wrong, it's impossible to say without seeing all of it.

Edit:

Now the code is added:

'ten = 0' needs to change to 'ten == 0' in order to do the comparison correctly.

you are missing a ')' on this line: (it should be after the zero)

if (twenty == 0 {

Also, I can't see where you've declared the variables ten and twenty, so try changing to this:

int ten = rnd.nextInt(10);
int twenty = rnd.nextInt(20);

Finally, none of this code is in a method, it's just within the class file. Is it supposed to be invoked statically when the class is constructed? or when a method is called?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top