简体   繁体   中英

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. If I remove the do while loop the code runs perfectly. 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.

Your problem is the Console.Clear statement that you run at start of do while loop. Comment that line and you will see that your code is going to 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.

There is no memory problem as you suspected.

The do while loop should have Console.Clear commented as in code below.

 do {

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

add a Console.ReadLine(); before break case "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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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