带环的外壳开关

时间:2014-01-08 04:51:53

标签: c# loops

我不了解我的case语句中发生了什么,以确定是否要重做用户输入。我应该在while循环之外创建另一个循环吗?我试过这样,我的case语句变成无法访问的代码。也许我不理解case-switch语句。

class Program
{
    static void Main(string[] args)
    {


        string _a = "";
        constructor con = new constructor();
        Console.WriteLine("Enter enter exit to end the program...");
        Console.WriteLine("Enter C for constructor, M for method, A for an array...");
        Console.WriteLine("Please reference source code to have full details and understanding...");
        bool control = true;
        while (control)
        {
            _a = Console.ReadLine();
            switch (_a.ToUpper())
            {

                case "EXIT":
                    Console.WriteLine("Thank you for using AJ's program...");
                    control = false;
                    break;
                case "C":
                    Console.WriteLine(con.a);
                    Console.WriteLine("Would you like to test another scenario?");
                    Console.ReadLine();
                    if (_a.ToUpper() == "Y")
                    {
                        Console.ReadLine();
                        return;

                    }
                    control = false;
                    break;
                case "M":
                    control = false;
                    metroid();
                    break;
                case "A":
                    control = false;
                    Array();
                    break;
                default: Console.WriteLine("No match");
                    break;
            }
        }
    }
    public class constructor
    {
        public string a = "This is a constructor!";
    }
    static public void metroid()
    {
        string b = "This is a method!";
        Console.WriteLine(b);
    }
    static public void Array()
    {
        try
        {
            Console.WriteLine("This is a random array. Please enter the size.");
            string sSize = Console.ReadLine();
            int arraySize = Convert.ToInt32(sSize);
            int[] size = new int[arraySize];
            Random rd = new Random();
            Console.WriteLine();
            for (int i = 0; i < arraySize; i++)
            {
                size[i] = rd.Next(arraySize);
                Console.WriteLine(size[i].ToString());
            }
        }
        catch (System.FormatException)
        {
            Console.WriteLine("Not correct format, restarting array process.");
            Array();
        }
    }
}
}

5 个答案:

答案 0 :(得分:1)

这就是我想出的。你有很多退出循环的方法,所以删除了所有control = false行,除非用户键入“EXIT”

此外,如果选择“Y”,如果“C”返回方法,我将其更改为continue,以便循环继续。

最后,我将3条指令语句移动到循环中,所以当用户点击“Y”时,它会再次打印出来。

static void Main(string[] args)
{
    string _a = "";
    constructor con = new constructor();
    bool control = true;
    while (control)
    {
        Console.WriteLine("Enter enter exit to end the program...");
        Console.WriteLine("Enter C for constructor, M for method, A for an array...");
        Console.WriteLine("Please reference source code to have full details and understanding...");
        _a = Console.ReadLine();
        switch (_a.ToUpper())
        {

            case "EXIT":
                Console.WriteLine("Thank you for using AJ's program...");
                control = false;
                break;
            case "C":
                Console.WriteLine(con.a);
                Console.WriteLine("Would you like to test another scenario?");
                _a = Console.ReadLine(); //<==problem #1 you didnt set your var name
                if (_a.ToUpper() == "Y")
                {
                    continue; //<==problem #2 return exits the program, continue, just keeps going
                }
                control = false;
                break;
            case "M":
                metroid();
                break;
            case "A":
                Array();
                break;
            default:
                Console.WriteLine("No match");
                break;
        }
    }
}

答案 1 :(得分:0)

可能你想改变

                Console.ReadLine();
                if (_a.ToUpper() == "Y")
                {
                    Console.ReadLine();
                    return;

                }

作为

                _a = Console.ReadLine();
                if (_a.ToUpper() == "Y")
                {
                    _a = Console.ReadLine();
                    continue;

                }

答案 2 :(得分:0)

我认为在这种情况下你应该考虑goto。是的,你需要付出额外的努力,但它会帮助你克服While循环。

以下示例:

 switch (_a.ToUpper())
  {
    case "EXIT":
      Console.WriteLine("Thank you for using AJ's program...");
      control = false;
      // execute goto when your all line executes successfully
      goto case "New";

     case "New":
     // some logic
  }

请在此处查看工作示例Goto-Switch

答案 3 :(得分:0)

                string NewInput= Console.ReadLine();
                if (NewInput.ToUpper() == "Y")
                {
                   //print some thing with console.writeline

                   //if after this you want to restart the loop then instead of return use 
                   continue;
                }

答案 4 :(得分:0)

尝试将Console.Writeline放在while循环中,如下所示:

static void Main(string[] args)
{
    bool control = true;
    while (control)
    {
        Console.WriteLine("Enter enter exit to end the program...");
        Console.WriteLine("Enter C for constructor, M for method, A for an array...");
        Console.WriteLine("Please reference source code to have full details and understanding...");
        string _a = Console.ReadLine();
        switch (_a.ToUpper())
        {
            case "EXIT":
                Console.WriteLine("Thank you for using AJ's program...");
                control = false;
                break;
            case "C":
                Console.WriteLine("press c");
                Console.WriteLine("Would you like to test another scenario?");
                Console.ReadLine();
                if (_a.ToUpper() == "Y")
                {
                    Console.ReadLine();
                    return;

                }
                control = false;
                break;
            case "M":
                control = false;
                metroid();
                break;
            case "A":
                control = false;
                Array();
                break;
            default: Console.WriteLine("No match");
                break;
        }
    }
}

有关切换herehere的其他说明。

只需为结果添加评论,谢谢。希望这有帮助!