Mr. Skadoosh
Mr. Skadoosh

Reputation: 11

How to decrement a value in an array in C#?

Assuming there's a three heart stored in an array.

string lives = new string [3] {"<3", "<3", "<3"};

This is intended for a quiz game. How do I remove a value from the lives array whenever the user inputs wrong answers?

For ex: Lives is <3<3<3, but since the user input is wrong, lives became <3<3.

I'm expecting a for loop and decrement for this. I'm very puzzled as I am still new to for loops.

A new solution or help would be very much appreciated! Thanks!

Upvotes: 0

Views: 356

Answers (2)

MakePeaceGreatAgain
MakePeaceGreatAgain

Reputation: 37000

As suggested in the comments you should separata your data from its represenation. Your data is just a number, the representation is a nice sweety symbol, which you just print multiple times.

Then you can decrement the number of lives and just render the symbols afterwards:

var lives = int;
// something that makes your lives decrease
lives--;
for(int i = 0; i < lives; i++) // print the symbol multiple times
    Console.Write("<3");

Upvotes: 2

Sardelka
Sardelka

Reputation: 483

You can use Array.Resize, as provided by the framework itself.

Array.Resize(lives, lives.Length-1)
if(lives.Length <= 0){
   Console.WriteLine("You're dead");
}

Or use List:

var lives = new List<string>() {"<3", "<3", "<3"};
lives.RemoveAt(lives.Count-1);

Upvotes: 0

Related Questions