using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace C_Sharp
{
    class Program
    {
            static bool Self()
            {
                Console.WriteLine("1、个人信息");
                Console.WriteLine("2、修改密码");
                Console.WriteLine("3、返回上级");
                Console.WriteLine("请输入数字");
                int myIntSelf;
                myIntSelf=Convert.ToInt32(Console.ReadLine());
                if (myIntSelf == 3)
                    return false;
                else
                    return true;
            }
            static bool Friend()
            {
                Console.WriteLine("1、添加好友");
                Console.WriteLine("2、删除好友");
                Console.WriteLine("3、返回上级");
                Console.WriteLine("请输入数字");
                int myIntFriend;
                myIntFriend=Convert.ToInt32(Console.ReadLine());
                if (myIntFriend == 3)
                    return false;
                else
                    return true;
            }
            static void Main(string[] args)
            {
                //约定,好友管理 Friend,个人管理 Self。
                //显示主界面
                Console.WriteLine("1、个人管理");
                Console.WriteLine("2、好友管理");
                Console.WriteLine("3、退出系统");
                Console.WriteLine("请输入数字");
                int myCase = Convert.ToInt32(Console.ReadLine());
                bool BoolFriend = true, BoolSelf = true, BoolMain = true;
                while(BoolMain)
                {
                    if (myCase == 3)
                    {
                        Console.WriteLine("拜拜");
                        BoolMain = false;
                    }
                    else if (myCase >= 1 && myCase <= 2)
                    {
                        switch (myCase)
                        {
                            case 1:
                                while (BoolSelf)
                                    BoolSelf = Self();
                                break;
                            case 2:
                                while (BoolFriend)
                                    BoolFriend = Friend();
                                break;
                            default:
                                Console.WriteLine("输入错误,请重新输入");
                                myCase = Convert.ToInt32(Console.ReadLine());
                                break;
                        }
                    }
                    else
                    {
                        Console.WriteLine("输入错误,请重新输入");
                        myCase = Convert.ToInt32(Console.ReadLine());
                    }
                }
       
            }
    }
}代码很短,而且只是一个架子,我发现当我进入二级菜单之后,不能返回上级界面。只要输入3,程序立马停止,必须用鼠标点击窗口来关闭。请问各位高手,我的错误在哪里?

解决方案 »

  1.   

    楼主这个不好慢慢改,发个代码,你自己对照着看吧namespace ConsoleApplication2
    {
        class Program
        {
            [STAThread]
            static void Main(string[] args)
            {
                //约定,好友管理 Friend,个人管理 Self。
                //显示主界面
                bool BoolMain = true;
                while (BoolMain)
                {
                    Console.WriteLine("1、个人管理");
                    Console.WriteLine("2、好友管理");
                    Console.WriteLine("3、退出系统");
                    Console.WriteLine("请输入数字");
                    int myCase = Convert.ToInt32(Console.ReadLine());
                    if (myCase == 3)
                    {
                        Console.WriteLine("拜拜");
                        BoolMain = false;
                    }
                    else if (myCase >= 1 && myCase <= 2)
                    {
                        switch (myCase)
                        {
                            case 1:
                                //while (BoolSelf)
                                Self();
                                break;
                            case 2:
                                //while (BoolFriend)
                                Friend();
                                break;
                            default:
                                Console.WriteLine("输入错误,请重新输入");
                                myCase = Convert.ToInt32(Console.ReadLine());
                                break;
                        }
                    }
                    else
                    {
                        Console.WriteLine("输入错误,请重新输入");
                        myCase = Convert.ToInt32(Console.ReadLine());
                    }
                }
            }        static void Self()
            {
                while (true)
                {
                    Console.WriteLine("1、个人信息");
                    Console.WriteLine("2、修改密码");
                    Console.WriteLine("3、返回上级");
                    Console.WriteLine("请输入数字");
                    int myIntSelf;
                    myIntSelf = Convert.ToInt32(Console.ReadLine());
                    if (myIntSelf == 3)
                        return ;            }
            }
            static void Friend()
            {
                while (true)
                {
                    Console.WriteLine("1、添加好友");
                    Console.WriteLine("2、删除好友");
                    Console.WriteLine("3、返回上级");
                    Console.WriteLine("请输入数字");
                    int myIntFriend;
                    myIntFriend = Convert.ToInt32(Console.ReadLine());
                    if (myIntFriend == 3)
                        return ;
                    
                }
            }
        }
    }
      

  2.   

    using System;
    using System.Collections.Generic;
    using System.Text;namespace C_Sharp
    {
        class Program
        {
            static bool Self()
            {
                Console.WriteLine("1、个人信息");
                Console.WriteLine("2、修改密码");
                Console.WriteLine("3、返回上级");
                Console.WriteLine("请输入数字");
                int myIntSelf;
                myIntSelf = Convert.ToInt32(Console.ReadLine());
                if (myIntSelf == 3)
                {
                    Main(null);
                    return false;
                }
                else
                {
                    return true;
                }
            }
            static bool Friend()
            {
                Console.WriteLine("1、添加好友");
                Console.WriteLine("2、删除好友");
                Console.WriteLine("3、返回上级");
                Console.WriteLine("请输入数字");
                int myIntFriend;
                myIntFriend = Convert.ToInt32(Console.ReadLine());
                if (myIntFriend == 3)
                {
                    Main(null);
                    return false;
                }
                else
                {
                    return true;
                }
            }
            static void Main(string[] args)
            {
                //约定,好友管理 Friend,个人管理 Self。
                //显示主界面
                Console.WriteLine("1、个人管理");
                Console.WriteLine("2、好友管理");
                Console.WriteLine("3、退出系统");
                Console.WriteLine("请输入数字");
                int myCase = Convert.ToInt32(Console.ReadLine());
                bool BoolFriend = true, BoolSelf = true, BoolMain = true;
                while (BoolMain)
                {
                    if (myCase == 3)
                    {
                        Console.WriteLine("拜拜");
                        BoolMain = false;
                    }
                    else if (myCase >= 1 && myCase <= 2)
                    {
                        switch (myCase)
                        {
                            case 1:
                                while (BoolSelf)
                                   BoolSelf = Self();
                                break;
                            case 2:
                                while (BoolFriend)
                                    BoolFriend = Friend();
                                break;
                            default:
                                Console.WriteLine("输入错误,请重新输入");
                                myCase = Convert.ToInt32(Console.ReadLine());
                                break;
                        }
                    }
                    else
                    {
                        Console.WriteLine("输入错误,请重新输入");
                        myCase = Convert.ToInt32(Console.ReadLine());
                    }
                }        }
        }
    }
      

  3.   

    其实已经返回只是你没给返回后执行任何代码而已~~所以在返回FALSE前加上Main(null);
      

  4.   

    goto语句在这里派上用场 了,试一下。
      

  5.   

    看来,我对于整个程序的流程,并没有规划好。光顾着整个大背景,没有考虑好细节。
    感谢 sdfkfkd 对我思路的改进,并且让我看到当函数是void的时候,return仍然可以应用。
    感谢 xu56180825 对我错误的指出和回答,Main(null),哈,我太粗心了。
    肯来看我的程序,修改我的程序,帮助我进步。真是非常感谢。
      

  6.   

    我的改进,只不过是在while下面,添加了语句,让这个while有事可做。于是整个程序就毫无问题了。
        while(BoolMain)
                    {
                        Console.WriteLine("1、个人管理");
                        Console.WriteLine("2、好友管理");
                        Console.WriteLine("3、退出系统");
                        Console.WriteLine("请输入数字");
                        myCase = Convert.ToInt32(Console.ReadLine());
                        if (myCase == 3)
                        {
                            Console.WriteLine("拜拜");
                            BoolMain = false;
                        }
                        else if (myCase >= 1 && myCase <= 2)
                        {
                            switch (myCase)
                            {
                                case 1:
                                    while (BoolSelf)
                                        BoolSelf = Self();
                                    break;
                                case 2:
                                    while (BoolFriend)
                                        BoolFriend = Friend();
                                    break;
                                default:
                                    Console.WriteLine("输入错误,请重新输入");
                                    myCase = Convert.ToInt32(Console.ReadLine());
                                    break;
                            }
                        }
                        else
                        {
                            Console.WriteLine("输入错误,请重新输入");
                            myCase = Convert.ToInt32(Console.ReadLine());
                        }
                    }
    谢谢各位大神。