samedi 28 septembre 2019

Currently using C# and having trouble looping

I am currently having trouble looping the following code so that the else part asks the question again.

        String name, wtd, act, trav;

        Console.WriteLine("What is your name?");
        name = Console.ReadLine();

        Console.WriteLine("Hello {0}, What do you want to do today?", name);
        Console.WriteLine("1) Action\n2) Chilling\n3) Danger\n4) Good Food\n");

        int result, ppl;


        result = Convert.ToInt32(Console.ReadLine());


        if (result == 1)
        {
            wtd = "action";
            act = "Stock Car Racing";
        }
        else if (result == 2)
        {
            wtd = "chilling";
            act = "Hiking";
        }
        else if (result == 3)
        {
            wtd = "danger";
            act = "Skydiving";
        }
        else if (result == 4)
        {
            wtd = "good food";
            act = "to Taco Bell";
        }
        else
        {
            wtd = "";
            act = "";
            Console.WriteLine("I do not understand. Please select again");

        }

Console.WriteLine("Okay. If you are in the mood for " + wtd + ", then you should go " + act + "and travel in " + trav +".");

*The rest of the code works fine. trav variable works fine

I have tried using while loop, but it always comes back with errors, or does not run how i want it to. One of the most common errors when using the while loop are "use of unassigned local variable wtd" and "use of unassigned local variable act". These errors happen at the last Console.WriteLine part of the code.

I also need the variables to be defined based on used input and would like it to loop if there is any invalid input, like any other value besides the ones listed (1,2,3,4).

Aucun commentaire:

Enregistrer un commentaire