如何在切换条件下使用百分比来根据健康状况来表示玩家状态?

时间:2019-03-23 05:54:24

标签: java

首先,我应该对编码和Java非常陌生;原谅我的愚蠢!我正在尝试根据玩家的健康状况为玩家建立一个状态系统,其中他们当前的Status是一个包含在切换功能中的String,其功能从“您做得很好!”到“你快死了!”。但是,我不想使用一位数字来执行此操作,而是希望使用生命值百分比来执行此操作,因此无论玩家的生命值上限为50还是10,当消息达到50%或25%时,消息都不会改变。

从本质上讲,我想这样做,而不是用单个变量表示开关功能中某些运行状况的值,即PCHealth的百分比。这是我不想要的,但是我知道该怎么做:

int PCHealth = 10;
String Status = "";
switch (PCHealth) {
  case 5: Status = "You're fine!";
  case 2: Status = "You're dying!";
}

这就是我要用伪代码勾勒出的内容:

int PCHealth = 10;
String Status = "';
switch (PCHealth) {
  case [50% of PCHealth] = "You're fine!";
  case [20 % of PCHealth] = "You're dying!";
}

谢谢!

4 个答案:

答案 0 :(得分:1)

/*

Things that you will need to know
- integer division vs double
- type casting
- Math.ceil()

# int division

10/25 --> 0

# double division

10.0/25 --> 0.4

# Math.ceil()

Math.ceil(0.4) --> 1.0



0-25 XXX
25-50 fine
50-75 good
75-100 great

*/

使用此代码,打印出您不了解的内容或它们持有的价值...我试图使事情变得简单。

public class MyClass {

    private static int maxHealth = 100;
    public static String getPlayerHealthStatus(double pHealth){

        String pHStatus = "";
        int pHealth_case = (int) Math.ceil( pHealth / (maxHealth / 4));

        switch (pHealth_case) {
            case 4:
                pHStatus = "You're doing great!"; 
                break;

            case 3:
                pHStatus = "You're doing good!";
                break;

            case 2:
                pHStatus = "You're fine!";
                break;

            case 1:
                pHStatus = "You're dying!";
                break;

            case 0:
                pHStatus = "You died!";
                break;

            default:
                pHStatus = "invalid player health";
        }

        return pHStatus;
    }

    public static void main(String args[]) {
        int playerHealth = 90;
        System.out.println("player health: "+playerHealth+" status: "+getPlayerHealthStatus(playerHealth));

        playerHealth = 70;
        System.out.println("player health: "+playerHealth+" status: "+getPlayerHealthStatus(playerHealth));

        playerHealth = 40;
        System.out.println("player health: "+playerHealth+" status: "+getPlayerHealthStatus(playerHealth));

        playerHealth = 20;
        System.out.println("player health: "+playerHealth+" status: "+getPlayerHealthStatus(playerHealth));

        playerHealth = 0;
        System.out.println("player health: "+playerHealth+" status: "+getPlayerHealthStatus(playerHealth));
    }
}

输出:

player health: 90 status: You're doing great!
player health: 70 status: You're doing good!
player health: 40 status: You're fine!
player health: 20 status: You're dying!
player health: 0 status: You died!

答案 1 :(得分:1)

只需使用if-else即可。它非常简单而且易于理解。

public String getStatus(int PChealth, int maxValue) {
    float percent = (PChealth * 1.0f) / maxValue;

    if (percent > 75) return "great";
    else if (percent > 50) return "mid";
    else if (percent > 25) return "not good";
    else if (PChealth == 0) return "dead";
    else return "bad";
}

旁注:评估if的第一个true将返回正确的值。
精明吗?

答案 2 :(得分:0)

我将最大健康状况存储在变量MaxHealth中,然后尝试以下开关! :)

if语句中有switch语句,如果运行状况在0到20%之间,则将执行情况1,依此类推

double PCHealth = __ ;

switch (((0 <= PCHealth && PCHealth <= (0.2 * MaxHealth)) ? 0 : 
        ((0.2 * MaxHealth) > PCHealth && (0.5 * MaxHealth) < PCHealth) ? 1 : 2)
  {

    case 0:
        Status = "You're dying!";
        break;
    case 1:
        Status = "You're fine!";
        break;
    case 2:
        Status = "You're doing excelent";
        break;
}

答案 3 :(得分:0)

在case语句中不能有表达式,不能使用if-else语句,也不能像@Onkar所说的那样将它们存储在单独的变量中。

if-else应该更适合您的用例。