Thread: quick if question

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    5

    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;
                                }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    51% of a number is that number times 0.51.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    % = 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;
    }
    Last edited by theoobe; 07-03-2009 at 05:40 PM.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    5
    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.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    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;
            }
        }
    }
    Last edited by theoobe; 07-04-2009 at 07:14 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM