Jump to content

Help with creating a Console Graph

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
Alex_j

Alex_j

    Newbie

  • Members
  • PipPip
  • 29 posts
I need to create a graph using the console with C#, it needs to be set out like below:

0 10 20 30 40 50 60 70 80 90 100
| | | | | | | | | | |
***************************************************************** Blue
**** Red
********************** Yellow

The data will be dependent upon what the user enters as the percentage has to be calculated, each asterisk represents 2%.

My question is how to do it? How can I get the correct amount of asterisk to be shown i.e if blue equals 40% then display 20 asterix.

Thanks

#2
FlashM

FlashM

    Learning Programmer

  • Members
  • PipPipPip
  • 90 posts

static void Main(string[] args)

{

    int percentage = 40;

    string percentageToString = new string('*', percentage / 2);

    Console.WriteLine(percentageToString);

    Console.ReadLine();

}



#3
Alex_j

Alex_j

    Newbie

  • Members
  • PipPip
  • 29 posts
Thanks that works fine apart from say if the number is 17% it needs to have 9 asterisk and this only makes it have 8, so it needs to round the number to the multiple of two in a way :confused:

Any ideas of how to do this?

#4
PGP_Protector

PGP_Protector

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 253 posts
add 1 to the percentage before you divide by 2 (I.E. force Rounding up)