Home > front end >  How can i write less with same result
How can i write less with same result

Time:05-03

Good afternoon everyone.

Can anyone help me, im new at Coding (15yo) so please dont hate.

I am trying to do an little text adventure, in C#, but i get more then 200 Lines, only if and else if, thats really frustrating because i want much usefull lines and not everything full spammed with if queries.

I would really appreciate it, if someone could help me, here is the part im Talking about (btw im from Germany so dont wonder about my english). I heard about the switch statement, but i dont know if this is the right case for it.

RPG.Inventar Inv = new RPG.Inventar();
            if (Inv_Numb == 1)
            {
                if (Inv.First_Place == 0)
                {
                    Console.WriteLine("Dieser Platz ist Frei");
                }
                else if (Inv.First_Place == 0001)
                {
                    Console.WriteLine("Here is an G36");
                }
                else if (Inv.First_Place == 0002)
                {
                    Console.WriteLine("Here is an M4A1");
                }
                else if (Inv.First_Place == 0003)
                {
                    Console.WriteLine("Here is an AK47");
                }
            }
            if (Inv_Numb == 2)
            {
                if (Inv.Second_Place == 0)
                {
                    Console.WriteLine("Dieser Platz ist Frei");
                }
                else if (Inv.Second_Place == 0001)
                {
                    Console.WriteLine("Here is an G36");
                }
                else if (Inv.Second_Place == 0002)
                {
                    Console.WriteLine("Here is an M4A1");
                }
                else if (Inv.Second_Place == 0003)
                {
                    Console.WriteLine("Here is an AK47");
                }
            }
            if (Inv_Numb == 3)
            {
                if (Inv.Third_Place == 0)
                {
                    Console.WriteLine("Dieser Platz ist Frei");
                }
                else if (Inv.Third_Place == 0001)
                {
                    Console.WriteLine("Here is an G36");
                }
                else if (Inv.Third_Place == 0002)
                {
                    Console.WriteLine("Here is an M4A1");
                }
                else if (Inv.Third_Place == 0003)
                {
                    Console.WriteLine("Here is an AK47");
                }
            }
            if (Inv_Numb == 4)
            {
                if (Inv.Fourth_Place == 0)
                {
                    Console.WriteLine("Dieser Platz ist Frei");
                }
                else if (Inv.Fourth_Place == 0001)
                {
                    Console.WriteLine("Here is an G36");
                }
                else if (Inv.Fourth_Place == 0002)
                {
                    Console.WriteLine("Here is an M4A1");
                }
                else if (Inv.Fourth_Place == 0003)
                {
                    Console.WriteLine("Here is an AK47");
                }
            }
            if (Inv_Numb == 5)
            {
                if (Inv.Fifth_Place == 0)
                {
                    Console.WriteLine("Dieser Platz ist Frei");
                }
                else if (Inv.Fifth_Place == 0001)
                {
                    Console.WriteLine("Here is an G36");
                }
                else if (Inv.Fifth_Place == 0002)
                {
                    Console.WriteLine("Here is an M4A1");
                }
                else if (Inv.Fifth_Place == 0003)
                {
                    Console.WriteLine("Here is an AK47");
                }
            }
            if (Inv_Numb == 6)
            {
                if (Inv.Sixth_Place == 0)
                {
                    Console.WriteLine("Dieser Platz ist Frei");
                }
                else if (Inv.Sixth_Place == 0001)
                {
                    Console.WriteLine("Here is an G36");
                }
                else if (Inv.Sixth_Place == 0002)
                {
                    Console.WriteLine("Here is an M4A1");
                }
                else if (Inv.Sixth_Place == 0003)
                {
                    Console.WriteLine("Here is an AK47");
                }
            }
            if (Inv_Numb == 7)
            {
                if (Inv.Seventh_Place == 0)
                {
                    Console.WriteLine("Dieser Platz ist Frei");
                }
                else if (Inv.Seventh_Place == 0001)
                {
                    Console.WriteLine("Here is an G36");
                }
                else if (Inv.Seventh_Place == 0002)
                {
                    Console.WriteLine("Here is an M4A1");
                }
                else if (Inv.Seventh_Place == 0003)
                {
                    Console.WriteLine("Here is an AK47");
                }
            }
            if (Inv_Numb == 8)
            {
                if (Inv.Eighth_Place == 0)
                {
                    Console.WriteLine("Dieser Platz ist Frei");
                }
                else if (Inv.Eighth_Place == 0001)
                {
                    Console.WriteLine("Here is an G36");
                }
                else if (Inv.Eighth_Place == 0002)
                {
                    Console.WriteLine("Here is an M4A1");
                }
                else if (Inv.Eighth_Place == 0003)
                {
                    Console.WriteLine("Here is an AK47");
                }
            }
            if (Inv_Numb == 9)
            {
                if (Inv.Ninth_Place == 0)
                {
                    Console.WriteLine("Dieser Platz ist Frei");
                }
                else if (Inv.Ninth_Place == 0001)
                {
                    Console.WriteLine("Here is an G36");
                }
                else if (Inv.Ninth_Place == 0002)
                {
                    Console.WriteLine("Here is an M4A1");
                }
                else if (Inv.Ninth_Place == 0003)
                {
                    Console.WriteLine("Here is an AK47");
                }
            }
            if (Inv_Numb == 10)
            {
                if (Inv.Tenth_Place == 0)
                {
                    Console.WriteLine("Dieser Platz ist Frei");
                }
                else if (Inv.Tenth_Place == 0001)
                {
                    Console.WriteLine("Here is an G36");
                }
                else if (Inv.Tenth_Place == 0002)
                {
                    Console.WriteLine("Here is an M4A1");
                }
                else if (Inv.Tenth_Place == 0003)
                {
                    Console.WriteLine("Here is an AK47");
                }

CodePudding user response:

A couple of tips:

  1. Inside your class Inventar, use an array to store the weapons for each spot. This way you can use a loop instead of the outer if (Inf_num == ...)s
  2. Use a dictionary for your choice of weapons. Using that removes most of the inner ifs such that only the check for an empty space remains
  3. Yes you can and should read up on the switch statement. It is not needed for this code sample but in generally useful when there are many ifs and else ifs.

CodePudding user response:

You can define an array with the messages in your class:

private static readonly string[] messagesByPlace = new[]
{
    "Dieser Platz ist Frei",
    "Here is an G36",
    "Here is an M4A1",
    "Here is an AK47"
};

A method to get the message:

private static string GetMessageByPlace(int place)
{
    if (place < 0 || place >= messagesByPlace.Length)
    {
        return null;
    }

    return messagesByPlace[place];
}

And use it:

string text = null;
switch (Inv_Numb)
{
    case 1: text = GetMessageByPlace(Inv.First_Place); break;
    case 2: text = GetMessageByPlace(Inv.Second_Place); break;
    case 3: text = GetMessageByPlace(Inv.Third_Place); break;
    case 4: text = GetMessageByPlace(Inv.Fourth_Place); break;
    case 5: text = GetMessageByPlace(Inv.Fifth_Place); break;
    case 6: text = GetMessageByPlace(Inv.Sixth_Place); break;
    case 7: text = GetMessageByPlace(Inv.Seventh_Place); break;
    case 8: text = GetMessageByPlace(Inv.Eighth_Place); break;
    case 9: text = GetMessageByPlace(Inv.Ninth_Place); break;
    case 10: text = GetMessageByPlace(Inv.Tenth_Place); break;
}

if (text != null)
{
    Console.WriteLine(text);
}
  • Related