随机生成器不起作用它总是显示0

时间:2014-04-07 21:16:21

标签: java

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

main函数包含在另一个类中:

package KotelezoProgram;

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

1 个答案:

答案 0 :(得分:0)

之间调用setHealth()
PouAbilites Pou = new PouAbilites();
Pou.onscreen();

呼叫

onscreen()之前调用setHealth()将返回0,因为this.Health刚刚在Pou对象中初始化为零。