Domanda

Couldn't find any other answer on the site that covers this. If else is run and an error message is displayed, is it possible to restart the program by looping back to the start instead of restarting the console?

class Program
{
    static void Main(string[] args)
    {
        int User;
        int Array;
        StreamWriter outfile = new StreamWriter("C://log.txt");
        Console.WriteLine("Input an number between 1 and 100");
        User = Convert.ToInt32(Console.ReadLine());
        if (User < 101 && User > 0)
        {
            for (Array = 1; Array <= User; Array++)
            {
                Console.WriteLine(Array + ", " + Array * 10 * Array);
                outfile.WriteLine(Array + ", " + Array * 10 * Array);
            }
            {
                Console.WriteLine("Press Enter To Exit The Console");
                outfile.Close();
                Console.ReadLine();
            }
        }

        else
            {
            Console.WriteLine("Sorry you input an invalid number. ");
            Console.ReadLine();
            }
    }
}

Sorry! To be more clear I need to make the Program start again if the user inputs an invalid number

Thanks for the help!

È stato utile?

Soluzione 2

An easy way of doing this is placing your code inside a while loop, so that the code keeps repeating. The only way to exit the loop would be for the condition you just set in the if clause to be true. So something along the lines of:

class Program
{
    static void Main(string[] args)
    {
        int User;
        int Array;
        bool isUserWrong = true;  //This is a flag that we will use to control the flow of the loop
        StreamWriter outfile = new StreamWriter("C://log.txt");

        while(isUserWrong)
        {
            Console.WriteLine("Input an number between 1 and 100");
            User = Convert.ToInt32(Console.ReadLine());
            if (User < 101 && User > 0)
            {
                for (Array = 1; Array <= User; Array++)
                {
                    Console.WriteLine(Array + ", " + Array * 10 * Array);
                    outfile.WriteLine(Array + ", " + Array * 10 * Array);
                }
                isUserWrong = false; // We signal that we may now leave the loop
            }
            else
            {
                Console.WriteLine("Sorry you input an invalid number. ");
                Console.ReadLine();
                //Note that here we keep the value of the flag 'true' so the loop continues
            }
        }
        Console.WriteLine("Press Enter To Exit The Console");
        outfile.Close();
        Console.ReadLine();
    }
}

Altri suggerimenti

You can do this instead

User = Convert.ToInt32(Console.ReadLine());
while (User >= 101 || User <= 0) 
{
    Console.WriteLine("Sorry you input an invalid number. ");
    User = Convert.ToInt32(Console.ReadLine());
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top