简体   繁体   English

随机生成器不起作用,它始终显示0

[英]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: main功能包含在其他类中:

package KotelezoProgram;

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

call setHealth() in between 之间调用setHealth()

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. setHealth() onscreen()之前调用onscreen()会返回0因为this.Health刚在Pou对象中初始化为零。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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