简体   繁体   中英

Random generator does not work it always shows 0

import java.util.Random;

public class PouAbilites {
    protected int Health;

    public int getHealth(){
        return this.Health;
    }

    public void setHealth(final int Health){
        final Random random  = new Random();

        final int randomInt = random.nextInt(100)+1;
        this.Health=randomInt;  
    }
    PouAbilites(){
       this.Eletero=getEletero();
    }
    public void onscreen(){
        System.out.println("Health: "+ this.Health);
    }
}

The main function is included in an other class:

package KotelezoProgram;

public class Main {
    public static void main(final String[] args) {
        final PouAbilites Pou = new PouAbilites();
        Pou.onscreen();
    }
}

call setHealth() in between

PouAbilites Pou = new PouAbilites();
Pou.onscreen();

calls

Calling onscreen() before setHealth() will return 0 because this.Health is just initialized in the Pou object to zero.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM