Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中的开关箱中不输入#_C#_Arrays_Struct_Switch Statement - Fatal编程技术网

C# 在C中的开关箱中不输入#

C# 在C中的开关箱中不输入#,c#,arrays,struct,switch-statement,C#,Arrays,Struct,Switch Statement,我无法输入switch语句的案例2,我不知道为什么。如果我删除do-while循环,代码将完美运行。这是关于结构数组的内存?代码如下: class Notebook { struct Student { public String id; public String name; public void showInfo(Student x) { Console

我无法输入switch语句的案例2,我不知道为什么。如果我删除do-while循环,代码将完美运行。这是关于结构数组的内存?代码如下:

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);

        }
    }
课堂笔记{
体类型
{
公共字符串id;
公共字符串名称;
公共信息(学生x){
Console.WriteLine(“\t ID:+x.ID”);
Console.WriteLine(“\t Name:+x.Name”);
}
}
静态void Main(字符串[]参数){
布尔显示=真;
int studentNum=int.Parse(Console.ReadLine());
学生[]学生=新学生[studentNum];
做{
Console.Clear();
控制台写入线(“1.-插入寄存器”);
控制台写入线(“2-显示寄存器”);
控制台写入线(“3-退出”);
字符串opc=Console.ReadLine();
交换机(opc){
案例“1”:
Console.Clear();
for(int i=0;i

我认为这是opc字符串内存中的“某物”,它避免了案例2

您的问题是
控制台。清除在do while循环开始时运行的
语句。注释该行,您将看到您的代码将进入案例“2”

即使在您的原始代码中也会出现case“2”,但每次在do while循环开始时都会清除控制台,所以您不会看到case“2”逻辑编写的语句

没有您怀疑的内存问题

do while循环应该有控制台。清除注释如下代码所示

 do {

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

您的问题是
控制台。请清除在do while循环开始时运行的
语句。注释该行,您将看到您的代码将进入案例“2”

即使在您的原始代码中也会出现case“2”,但每次在do while循环开始时都会清除控制台,所以您不会看到case“2”逻辑编写的语句

没有您怀疑的内存问题

do while循环应该有控制台。清除注释如下代码所示

 do {

            //Console.Clear();
            Console.WriteLine("1.- Insert register");
            Console.WriteLine("2.- Show register");
            Console.WriteLine("3.- Exit");
添加一个Console.ReadLine();破发前案例“2”

案例“2”:
Console.Clear();
for(int i=0;i
编写学生信息并调用Console.Clear(),然后添加Console.ReadLine();破发前案例“2”

案例“2”:
Console.Clear();
for(int i=0;i

编写学生信息并调用Console.Clear()之后

是否尝试过使用调试器?您的代码乍一看很好,但调试器将允许您查看程序流和变量所代表的对象的详细内容。只需执行调试即可。您能验证一下吗?@vasily.sib是的,我在设置值后写了一个Console.WriteLine(opc),并显示“2”和其他内容。如果我写了“3”,程序就会完美地完成。不,不要
Console.WriteLine(opc)
。调试并检查即时窗口中的if
opc==“2”
。@vasily.sib我编写了if语句,程序跳过它。好像它没有被写。你试过使用调试器吗?您的代码乍一看很好,但调试器将允许您查看程序流和变量所代表的对象的详细内容。只需执行调试即可。您能验证一下吗?@vasily.sib是的,我在设置值后写了一个Console.WriteLine(opc),并显示“2”和其他内容。如果我写了“3”,程序就会完美地完成。不,不要
Console.WriteLine(opc)
。调试并检查即时窗口中的if
opc==“2”
。@vasily.sib我编写了if语句,程序跳过它。好像它没有被写出来。这是完美的作品!我需要对我的代码更加小心。是的,这种类型的问题非常微妙,在编写代码时很容易出现。这非常有效!我需要更加小心我的代码。是的,这类问题非常微妙,在编写代码时很容易出现。