Reputation: 1
I should preface this by saying I'm VERY new to coding and Java in general; forgive my stupidity! I'm attempting to set up a status system for the player based on how much health they have, wherein their current Status is a String involved in a switch function which changes from "You are doing fine!" to "You are dying!". However, rather than doing it with single digits, I want to do it with percentage of health so the message does not change when they reach 50 or 25 percent health, whether the player has 50 or 10 health max.
Essentially, I want to make it so in place of single variables representing certain values of health in the switch function, percentages of PCHealth do. This is what I don't want, but what I know how to do:
int PCHealth = 10;
String Status = "";
switch (PCHealth) {
case 5: Status = "You're fine!";
case 2: Status = "You're dying!";
}
Here's what I want sketched out in fake code:
int PCHealth = 10;
String Status = "';
switch (PCHealth) {
case [50% of PCHealth] = "You're fine!";
case [20 % of PCHealth] = "You're dying!";
}
Thank you!
Upvotes: 0
Views: 674
Reputation: 814
/*
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
*/
Play with this code, printout things that you don't understand or what value they are holding... I tried to keep things simple.
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));
}
}
Output:
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!
Upvotes: 1
Reputation: 7871
Just do it with if-else
. Its very simple and good to understand.
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";
}
Side notes: The first if
that evalute true
will return the correct value.
Savvy?
Upvotes: 1
Reputation: 147
I would store the max health in the variable MaxHealth
then try the following switch! :)
Here we have if
statement in the switch
statement, that will do case 1 if the health is between 0 and 20% and so on
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;
}
Upvotes: 0
Reputation: 1334
Its not possible to have expressions in case statements, use if-else
statements or like @Onkar said store them in a separate variable.
if-else
should be cleaner for your use case.
Upvotes: 0