在此之前,我应该说,我对编程和Java总体来说是非常陌生的;请原谅我的愚蠢!我正在尝试为玩家设置一个基于他们的健康程度的状态系统,其中他们的当前状态是一个切换函数中涉及的字符串,该字符串从"You are doing fine!“变成“你快死了!”然而,不是用个位数,我想用健康百分比来做,这样当他们达到50%或25%的生命值时,消息就不会改变,无论玩家的最大生命值是50%还是10%。
本质上,我想让它取代开关函数中代表某些健康值的单变量,即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!";
}谢谢!
发布于 2019-03-23 14:38:57
/*
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!发布于 2019-03-23 15:47:13
只要用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";
}附注:对true求值的第一个if将返回正确的值。
懂吗?
发布于 2019-03-23 14:36:17
我会将最大健康值存储在变量MaxHealth中,然后尝试以下开关!:)
在这里,我们在switch语句中有if语句,如果运行状况在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;
}https://stackoverflow.com/questions/55311044
复制相似问题