简体   繁体   English

不使用C#输入开关

[英]Not enter in a switch case in C#

I can't enter in the case 2 of the switch statement and I don't know why. 我无法输入switch语句的情况2,也不知道为什么。 If I remove the do while loop the code runs perfectly. 如果删除do while循环,则代码可以完美运行。 It's about something with the memory of the structure array? 这与结构数组的内存有关吗? Here is the code: 这是代码:

class Notebook {

        struct Student
        {
            public String id;
            public String name;
            public void showInfo(Student x) {
                Console.WriteLine("\t ID: " + x.id);
                Console.WriteLine("\t Name: " + x.name);
            }

        }
        static void Main(string[] args){

            bool display = true;

            int studentNum = int.Parse(Console.ReadLine());

            Student[] students = new Student[studentNum];

            do {

                Console.Clear();
                Console.WriteLine("1.- Insert register");
                Console.WriteLine("2.- Show register");
                Console.WriteLine("3.- Exit");


                String opc = Console.ReadLine();


                switch (opc) {

                    case "1":
                        Console.Clear();
                        for(int i = 0; i < students.Length; ++i){
                            Console.WriteLine("Name of the student " + (i+1));
                            students[i].name = Console.ReadLine();
                            Console.WriteLine("ID of the student " + (i+1));
                            students[i].id = Console.ReadLine();
                        }
                        break;

                    case "2":
                        Console.Clear();
                        for(int i = 0; i < students.Length; ++i){
                            students[i].showInfo(students[i]);
                        }
                        break;

                    case "3":
                        Console.Clear();
                        Console.WriteLine("bye");
                        display = false;
                        break;

                }

            }while(display);

        }
    }

I think that is "something" in the memory of opc string that avoids the case 2. 我认为这是避免情况2的opc字符串存储中的“某物”。

Your problem is the Console.Clear statement that you run at start of do while loop. 您的问题是在do while循环开始时运行的Console.Clear语句。 Comment that line and you will see that your code is going to case "2" . 注释该行,您将看到您的代码将进入case "2"

Its going to case "2" even in your original code, but console is every time being cleared at start of do while loop and so you don't see the statements written by case "2" logic. 即使在原始代码中,它也将变为大小写“ 2”,但是每次在do while循环开始时都会清除控制台,因此您看不到大小写“ 2”逻辑编写的语句。

There is no memory problem as you suspected. 您怀疑没有内存问题。

The do while loop should have Console.Clear commented as in code below. do while循环应具有Console.Clear注释,如下面的代码所示。

 do {

            //Console.Clear();
            Console.WriteLine("1.- Insert register");
            Console.WriteLine("2.- Show register");
            Console.WriteLine("3.- Exit");

add a Console.ReadLine(); 添加一个Console.ReadLine(); before break case "2". 中断情况“ 2”之前。

case "2":
                    Console.Clear();
                    for (int i = 0; i < students.Length; ++i)
                    {
                        students[i].showInfo(students[i]);
                    }
                    Console.ReadLine();
                    break;

You write students informations and Call Console.Clear() after that 您编写学生信息并在此之后调用Console.Clear()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM