-
quick if question
hey guys, here's my problem.
im making a text based rpg and i want to show the player's HP in green whenever his current hp is larger than 50% of the max hp.
as well as showing it in yellow when his hp ranges from 49% to 26%.
and also showing it in red whenever it's lower than 25% of the max hp.
My code works fine whenever you have more than 50%, but it keeps showing it in green all the way down to 25%, it basically bypasses the yellow command and goes right to red.
here's my code, hope someone can help me.
(this isn't homework btw i'm doing this on my own)
the max hp is 40.
Code:
if (ok.player > 51% ok.maxhp)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("{0}/{1} HP", ok.player, ok.maxhp);
Console.ForegroundColor = ConsoleColor.White;
}
else
if ((ok.player >= 26% ok.maxhp) && (ok.player <= 50% ok.maxhp))
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("{0}/{1} HP", ok.player, ok.maxhp);
Console.ForegroundColor = ConsoleColor.White;
}
else
if (ok.player < 25% ok.maxhp)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("{0}/{1} HP", ok.player, ok.maxhp);
Console.ForegroundColor = ConsoleColor.White;
}
-
51% of a number is that number times 0.51.
-
% = modulus.
Try this solution:
Code:
decimal one_percent = (decimal)(ok.maxhp / 100);
decimal value = (decimal)ok.player;
if (value > (one_percent * 51))
{
// ok.player is greater than 51% of ok.maxhp
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("{0}/{1} HP", ok.player, ok.maxhp);
Console.ForegroundColor = ConsoleColor.White;
}
else if (value >= (one_percent * 26) && value <= (one_percent * 50))
{
// ok.player is between 26% and 50% of ok.maxhp
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("{0}/{1} HP", ok.player, ok.maxhp);
Console.ForegroundColor = ConsoleColor.White;
}
else if (value < (one_percent * 25))
{
// ok.player is less than 25% of ok.maxhp
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("{0}/{1} HP", ok.player, ok.maxhp);
Console.ForegroundColor = ConsoleColor.White;
}
-
Thanks a lot for the replies.
I tried your solution and now the HP stays in green all the way down to 0 HP.
do i have to use double when dealing with percentages? i changed the int for player hp to double and it still doesn't work.
your code is probably right i'm sure i got something else wrong.
-
OK. Let's try something a little bit different. I've actually tried this code for myself, so I know it works!!
Code:
using System;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
Console.ForegroundColor = GetColor(24, 100); // should be red
Console.WriteLine("some red text");
Console.ForegroundColor = GetColor(55, 100); // should be green
Console.WriteLine("some green text");
Console.ForegroundColor = GetColor(40, 100); // should be yellow
Console.WriteLine("some yellow text");
Console.Read();
}
static ConsoleColor GetColor(int player, int maxhp)
{
decimal percent = (decimal)player;
percent /= maxhp;
percent *= 100;
if (percent > 50) // over 50%
return ConsoleColor.Green;
if (percent < 25) // less than 25%
return ConsoleColor.Red;
// must be between 26% and 50%
return ConsoleColor.Yellow;
}
}
}