【问题标题】:While loop not working while using bool in C#在 C# 中使用 bool 时,while 循环不起作用
【发布时间】:2014-04-28 02:00:36
【问题描述】:

我的 while 循环不工作。菜单上有 3 个选项,用户不断输入输入,直到按下“esc”。但是程序在用户输入其中一个选项后退出,并且不会返回菜单。如何修复我的 while 循环?请帮忙。

bool runApp = false;//menu.cs #12 Need to false sinces the loop is true
        Student[] students = new Student[35];
        //Application loop
        while (runApp);
        {
            Console.Clear();
            Console.WriteLine("\n\tGrade Book Menu\n");
            Console.WriteLine("\t1) Add Student");
            Console.WriteLine("\t2) Enter Student Grades");
            Console.WriteLine("\t3) List Student Grades");
            Console.Write("\nEnter Selection or Press Escape to exit: ");
            ConsoleKeyInfo key = Console.ReadKey();
            if (key.Key == ConsoleKey.Escape)
            {
                runApp = false;
            }
            else
            {
                switch (key.Key)
                {
                    case ConsoleKey.NumPad1:
                    case ConsoleKey.D1:
                        //Get the current student count stored in the Student Class variable
                        int indexForNewStudent = Student.GetStudentCount();
                        indexForNewStudent = 0;
                        Console.Write("\nEnter Student Name: ");
                        //Instantiate a Student object and place it in the array of Student objects called student
                        students[indexForNewStudent] = new Student(Console.ReadLine()); //Call overloaded constructor
                        //Increment Student count
                        Student.SetStudentCount(indexForNewStudent + 2); //Add to index to account for new student
                        break;
                    case ConsoleKey.NumPad2:
                    case ConsoleKey.D2:
                        Console.WriteLine("\nEnter the Student Number.  Use List Students to get Student Number.");
                        int studentNumber = 0; //Temporary variable to hold the student number to enter grades
                        //Test the entered string is a number and between 1 and 30
                        if ((int.TryParse(Console.ReadLine(), out studentNumber)) && (studentNumber <= 1) &&
                             (studentNumber >= 30))
                        {
                            //In the event a student has not been added this code will crash
                            if (Student.GetStudentCount() < 0) //Has a student been added?
                                students[studentNumber - 1].EnterStudentScores(); //Subtract 1 from enterd number for array index
                            else
                                Console.WriteLine("A student has not been added");
                        }
                        else
                        {
                            Console.WriteLine("Invalid Student Number.  Enter a number from 1 to 30");
                        }
                        break;
                    case ConsoleKey.NumPad3:
                    case ConsoleKey.D3:
                        Student.ListStudents(students);
                        break;
                    default:
                        Console.WriteLine("Invalid Menu Selection");
                        break;
                }
                Console.Write("Press a key to return to Menu");
                Console.ReadKey();
            }
        }
        Console.Write("\nExiting Application.  Press any key to close window... ");
        Console.ReadKey();

【问题讨论】:

  • 这根本不应该工作。在点击 while 循环之前,您的布尔标志设置为 false...
  • 你知道 while 循环是如何工作的吗?

标签: c# while-loop boolean


【解决方案1】:

你有两个问题:

  1. “runApp”变量需要初始化为真。这允许循环运行,直到“escape”键将其设置为 false。

  2. 删除while(runApp) 之后的分号。这会导致循环一直旋转,直到条件为假(这可能是变量首先设置为假的原因)。删除它允许执行 {} 中的代码,直到条件变为 false(这发生在按下退出键时。

如果我能澄清这些,请告诉我!

【讨论】:

    【解决方案2】:

    将 runApp 初始化为 true 并通过删除分号“;”来修复此行

    while (runApp);
    

    应该是

    while (runApp)
    

    【讨论】:

      【解决方案3】:

      将runApp改为true:

      bool runApp = true;
      

      【讨论】:

      • 这错过了while循环后的分号
      • 这还不够。在 while 语句之后还有一个分号。
      【解决方案4】:

      问题出在第一行:

      bool runApp = true; //this condition needs to be true or it won't enter
              Student[] students = new Student[35];
              //Application loop
              while (runApp) //you need no ";" or it won't execute the loop
      
      1. runApp 必须为真;
      2. 您需要删除分号,否则括号中的代码不会循环。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多