C#控制台程序循环问题

时间:2013-10-24 23:26:53

标签: c#

我尝试创建一个C#程序,为用户提供3个选项:

  1. 创建名称(让用户输入他们的名字和姓氏,并将其显示为J.Blogg)

  2. 一个数字的因子(输出为例如5x4x3x2x1 = 120,这是5的阶乘

  3. 退出

  4. 我让程序运行正常但是当我尝试选择选项1(创建名称)然后选项2时它转到选项1而不是它不允许我退出(选项3)。 我是编程的新手,所以它很简单,但我无法看到我出错的地方, 任何帮助都会非常棒。 我想保持相同的布局,我认为我的问题可能是循环,但任何帮助和改进都会很棒。

        static void Main(string[] args)
        {
    
            //The value returned from the topmenu method is stored in a variable called useroption
            int useroption;
            useroption = topmenu();
    
            // excute while loop untill option is not 1-3
            do
            {
    
    
                if (useroption == 1)
                {
                    Console.Clear();
                    Createname();
                    //break;
                }
    
                if (useroption == 2)
                {
                    Console.Clear();
                    factorial();
                   // break;
                }
    
                if (useroption == 3)
                {
                    Console.Clear();
                    Console.WriteLine("Thank you for using my program, Good bye !!!");
                   // break;
                }
    
                //topmenu();
                 } 
            while (useroption != 3);
    
    
    
    
            Console.ReadKey();
    
    
        }
    
        //This method present the user with an menu which the user has a choice of 3 options
        static int topmenu()
        {
            int option;
            string option_str;
    
            Console.Clear();
            Console.WriteLine("********************************************************************************");
            Console.WriteLine("********************************************************************************");
            Console.WriteLine("*********      OPTION 1 : Enter your name                              *********");
            Console.WriteLine("*********      OPTION 2 : Enter the number you want to factorise       *********");
            Console.WriteLine("*********      OPTION 3 : Quit                                         *********");
            Console.WriteLine("********************************************************************************");
            Console.WriteLine("********************************************************************************");
            option_str = Console.ReadLine();
    
            option = Convert.ToInt32(option_str);
            Console.Clear();
    
            if (option < 0 || option > 3)
            {
                Console.WriteLine("You have enter an invald option,");
                Console.WriteLine("Please chose a option between 1-3 (Please press any key to return to main menu)");
                Console.ReadLine();
                Console.Clear();
                topmenu();
            }
            else
            {
                Console.WriteLine("You have chosen option: " + option + " (Please press any key continue)");
            }
            Console.ReadKey();
            return option;
    
    
    
    
        }
        //this method asks user to enter their name (1st name then surname) and presents it back to the user as their intial(1st name) and surname
        static void Createname()
        {
            string firstname, surname, firstname_str, surname_str, userfullname;
    
            Console.Clear();
            Console.WriteLine("Please enter your first name ");
            firstname_str = Console.ReadLine();
            firstname = Convert.ToString(firstname_str);
            Console.Clear();
            Console.WriteLine("Please enter your surname name ");
            surname_str = Console.ReadLine();
            surname = Convert.ToString(surname_str);
            Console.Clear();
            userfullname = firstname + surname;
    
            Console.WriteLine("You have entered your name as " + firstname[0] + "." + surname);
            Console.WriteLine("(Please press any key to return to main menu)");
            Console.ReadKey();
            topmenu();
    
        }
    
        //this method asks the user to enter a number and returns the factorial of that number
        static double factorial()
        {
    
    
            string number_str;
            double factorial = 1;
    
    
    
            Console.WriteLine("Please enter number");
            number_str = Console.ReadLine();
    
            int num = Convert.ToInt32(number_str);
    
    
            // If statement is used so when the user inputs 0, INVALID is outputed
    
            if (num <= 0)
            {
                Console.WriteLine("You have enter an invald option");
                Console.WriteLine("Please enter number");
                number_str = Console.ReadLine();
                Console.Clear();
    
    
                num = Convert.ToInt32(number_str);
                //Console.Clear();
                //topmenu();
                //number_str = Console.ReadLine();
            }
    
            if (num >= 0)
            {
    
                while (num != 0) 
                {
                    for (int i = num; i >= 1; i--)
                    {
                        factorial = factorial * i;
                        Console.Write(i + " * ");
    
                    }
    
    
                        Console.WriteLine("= "+factorial+ " which is factorial of " + number_str.ToString() );
                        Console.WriteLine("(please any key to return to main menu)");
                        Console.ReadKey();
                        Console.Clear();
                        topmenu();
    
                }
    
            }
    
    
            return factorial;
    
    
    
    
        }
    }
    

    }

3 个答案:

答案 0 :(得分:3)

只需将这些行放入...中

int useroption;
useroption = topmenu();

重新安排如下......

int useroption;
        // excute while loop untill option is not 1-3
        do
        {

            useroption = topmenu();

您的程序将正常运行

完整代码在此处:http://pastebin.com/fCh0ttUY

答案 1 :(得分:0)

首先,在执行一些代码后将useroption设置为0。否则它将继续执行它。 第二,ReadKey()就在你的while语句之前。否则您将无法读取输入。

答案 2 :(得分:0)

问题在于,虽然您再次显示topmenu,但您永远不会重新分配useroption的值。

正如gypsyCoder所说,在do {} while()块中移动菜单显示将解决您的问题,因为它会导致每次循环时重新分配使用。