How can I make a program loop back to the start if else is true? C#

会有一股神秘感。 提交于 2021-01-29 04:31:22

问题


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!


回答1:


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();
    }
}



回答2:


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());
}



回答3:


This would be a good use for the do-while loop in Java:

class DoWhileDemo {
    public static void main(String[] args){
        int count = 1;
        do {
            System.out.println("Count is: " + count);
            count++;
        } while (count < 11);
    }
}


来源:https://stackoverflow.com/questions/19775020/how-can-i-make-a-program-loop-back-to-the-start-if-else-is-true-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!