Console.Clear只清除某些选择?

时间:2017-06-10 22:30:49

标签: c# debugging

我有一个菜单。基本上,我需要修复此错误。到目前为止我工作得很好。在Console.Clear开启的情况下,我只能使用1或8. 2-7只是清除。它应该显示“你需要先创建一个学生”。如果我关闭Console.Clear,那么它可以工作。但是,我需要它,因为这是重置菜单的部分。此外,它还会运行仅在您退出时运行的行。我没有改变任何重大的东西。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GradeCalculator
{
    class Program
    {
        static void Main(string[] args)
        {
            Student currentStudent = null;

        bool running = true; // Logic 1
        string input = "";

        while(running)
        {
            // display menu
            Console.Clear();
            Console.WriteLine("Main menu: ");
            Console.WriteLine("1. Create a student");
            Console.WriteLine("2. Add a course to the current student");
            Console.WriteLine("3. Remove a course from the current student");
            Console.WriteLine("4. Add grades for a course");
            Console.WriteLine("5. Display student info");
            Console.WriteLine("6. Display grades for a course");
            Console.WriteLine("7. Display all grades");
            Console.WriteLine("8. Exit");
            Console.Write("Enter a selection: (1 - 8): ");
            input = Console.ReadLine().ToLower();

            Console.WriteLine();
            // handle choices
            //------------------------------------------------------------------------
            switch(input)
            {
                case "1":
                case "create a student":
                    {
                        Console.Write("What is the students first name? ");
                        string firstName = Console.ReadLine();

                        Console.Write("What is the students last name? ");
                        string lastName = Console.ReadLine();
                        currentStudent = new Student(firstName, lastName);

                        Console.Write("How old is the student? ");
                        string studentAge = Console.ReadLine(); // Logic 2
                        int age;

                        while(!int.TryParse(studentAge, out age))
                        {
                            Console.Write("Please enter a number: ");
                            studentAge = Console.ReadLine();

                        }

                        currentStudent.Age = age;

                        Console.Write("What is the students address? ");
                        currentStudent.Address = Console.ReadLine();

                        Console.Write("What is the students email? ");
                        currentStudent.Email = Console.ReadLine();

                        Console.Write("What is the students phone number? ");
                        currentStudent.Phone = Console.ReadLine();
                    }
                    break;
                case "2":                      
                case "add a course to the current student":
                    {
                        if(currentStudent != null)
                        {
                            currentStudent.AddACourse();
                        }
                        else
                        {
                            Console.WriteLine("Please create a student first.");
                        }
                    }
                    break;
                case "3":
                case "remove a course from the current student":
                    {
                        if (currentStudent != null)
                        {
                            currentStudent.RemoveACourse();
                        }
                        else
                        {
                            Console.WriteLine("Please create a student first.");
                        }
                    }
                    break;
                case "4":
                case "add grades for a course":
                    {
                        if (currentStudent != null)
                        {
                            currentStudent.AddGradesForACourse();
                        }
                        else
                        {
                            Console.WriteLine("Please create a student first.");
                        }
                    }
                    break;
                case "5":
                case "display student info":
                    {
                        if (currentStudent != null)
                        {
                            currentStudent.DisplayInfo();
                        }
                        else
                        {
                            Console.WriteLine("Please create a student first.");
                        }
                    }
                    break;
                case "6":
                case "display grades for a course":
                    {
                        if (currentStudent != null)
                        {
                            currentStudent.DisplayGradesForACourse();
                        }
                        else
                        {
                            Console.WriteLine("Please create a student first.");
                        }
                    }
                    break;
                case "7":
                case "display all grades":
                    {
                        if (currentStudent != null)
                        {
                            currentStudent.DisplayAllGrades();
                        }
                        else
                        {
                            Console.WriteLine("Please create a student first.");
                        }
                    }
                    break;
                case "8":
                case "exit":
                    {
                        running = false; // Logic 6
                    }
                    break;
                default:
                    return;
            }


            Console.WriteLine("You have chosen to exit");
            //Console.ReadKey();
        }
    }
}
}

学生班

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GradeCalculator
{
    class Student
    {
        static int _nextStudentIDNum = 1000;

    string _firstName;
    string _lastName;
    string _email;
    string _address;
    string _phoneNumber;
    int _age;
    int _studentIdNum;
    List<Course> _courses;

    public Student(string firstName, string lastName)
    {
        _firstName = firstName;
        _lastName = lastName;
        _studentIdNum = ++_nextStudentIDNum;
        _courses = new List<Course>();
    }

    public string Name
    {
        get
        {
            return $"{_lastName}, {_firstName}";

        }
    }
    public string FirstName
    {
        get
        {
            return _firstName;
        }
        set
        {
            _firstName = value;
        }
    }
    public string LastName
    {
        get
        {
            return _lastName;
        }
        set
        {
            _lastName = value;
        }
    }
    public string Address
    {
        get
        {
            return _address;
        }
        set
        {
            _address = value;
        }
    }
    public string Email
    {
        get
        {
            return _email;
        }
        set
        {
            _email = value;
        }
    }
    public string Phone
    {
        get
        {
            return _phoneNumber;
        }
        set
        {
            _phoneNumber = value;
        }
    }
    public int Age
    {
        get
        {
            return _age;
        }
        set
        {
            _age = value;
        }
    }
    public int StudentNumber
    {
        get
        {
            return _studentIdNum;
        }
        set
        {
            _studentIdNum = value;
        }
    }
    public List<Course> Courses
    {
        get
        {
            return _courses;
        }
    }

    private int SelectCourse(string message)
    {
        int len = _courses.Count;
        int index = -1;

        if (len > 0) //Logic 3
        {
            for (index = 0; index < len; ++index)
            {
                Console.WriteLine($"{index + 1}. {_courses[index].Title}");
            }
            Console.Write(message);
            string selection = Console.ReadLine();

            while (!int.TryParse(selection, out index) || (index < 1 || index > len))
            {
                Console.Write("Please make a valid selection: ");
                selection = Console.ReadLine();
            }

            --index;
        }

        return index;
    }

    public void AddACourse()
    {
        string input;

        Console.Write("How many assignments are in the course? ");
        input = Console.ReadLine();
        int numAssignments = 0;

        while (!int.TryParse(input, out numAssignments))
        {
            Console.Write("Please enter a number: ");
            input = Console.ReadLine();
        }

        Course course = new Course(numAssignments);

        Console.Write("What is the courses title? ");
        course.Title = Console.ReadLine();

        Console.Write("What is the courses description? ");
        course.Description = Console.ReadLine();

        _courses.Add(course);
    }

    public void RemoveACourse()
    {
        int index = SelectCourse("Select a course to remove. (Enter the number): ");

        if (index == -1) //Logic 8
        {
            Console.WriteLine("No courses to remove.  Try adding one first.");
        }
        else
        {
            _courses.RemoveAt(index);
        }
    }

    public void AddGradesForACourse()
    {
        int index = SelectCourse("Select a course to add grades for. (Enter the number): ");

        if (index == -1)
        {
            Console.WriteLine("No course to add grades to.  Try adding one first.");
        }
        else
        {
            _courses[index].AddGrades();
        }
    }

    public void DisplayGradesForACourse()
    {
        int index = SelectCourse("Select a course to display grades for. (Enter the number): ");

        if (index == -1)
        {
            Console.WriteLine("No course to display grades for.  Try adding one first.");
        }
        else
        {
            _courses[index].DisplayGrades();
        }
    }

    public void DisplayAllGrades()
    {
        foreach (Course c in _courses)
        {
            c.DisplayGrades(); //Logic 7
        }
    }

    public void DisplayInfo()
    {
        Console.WriteLine($"Name: {Name}\nAge: {Age}\nAddress: {Address}\nPhone: {Phone}\nEmail: {Email}");
    }
}
}

课程班

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GradeCalculator
{
    class Course
    {
        string _title;
        string _description;
        Grade[] _grades;
        int _graded;

    public string Title
    {
        get
        {
            return _title;
        }
        set
        {
            _title = value;
        }
    }
    public string Description
    {
        get
        {
            return _description;
        }
        set
        {
            _description = value;
        }
    }
    public Grade[] Grades
    {
        get
        {
            return _grades;
        }
    }

    public Course(int numberOfAssignments)
    {
        _grades = new Grade[numberOfAssignments];
        _graded = 32;
    }

    public void AddGrades()
    {
        do
        {
            for (int i = 0; i < _grades.Length; ++i)
            {
                _grades[i] = new Grade();

                Console.Write($"Enter a description for assignment {i + 1}: ");
                _grades[i].Description = Console.ReadLine();


                string input;
                float value;

                Console.Write($"Enter the grade earned for assignment {i + 1} as a percentage (0 - 100): ");
                input = Console.ReadLine();

                while (!float.TryParse(input, out value)) // Logic 4
                {
                    Console.WriteLine("Please enter a number: ");
                    input = Console.ReadLine();
                }

                _grades[i].PercentEarned = value;

                Console.Write($"Enter assignment {i + 1}'s weight of total grade as a percentage (0 - 100): ");
                input = Console.ReadLine();

                while (!float.TryParse(input, out value))
                {
                    Console.WriteLine("Please enter a number: ");
                    input = Console.ReadLine();
                }
                _grades[i].Weight = value;
            }
        }
        while (ValidateWeightTotal() == false); 

            _graded = 0;


    }

    private bool ValidateWeightTotal()
    {
        float precisionFactor = 0.001f;
        float _totalWeight = 0;
        bool result = false;

        for (int i = 0; i < _grades.Length; ++i)
        {
            _totalWeight += _grades[i].Weight;
        }

        if(_totalWeight < 100 + precisionFactor && _totalWeight > 100 - precisionFactor)
        {
            result = true;
        }

        if (result == false)
        {
            Console.WriteLine($"Weight total = {_totalWeight} instead of 100.\nPlease enter the values again.\n");
        }

        return result;
    }

    public float GetFinalGrade()
    {
        bool weightsAreValid = ValidateWeightTotal();
        float finalGrade = 0;

        if(weightsAreValid)
        {
            for (int i = 0; i < _grades.Length; ++i)
            {
                finalGrade += _grades[i].GetPercentOfFinalGrade();
            }
        }

        return finalGrade;
    }

    public void DisplayGrades()
    {
        if (_graded == 0)
        {
            float total = 0f;
            Console.WriteLine("-------------------------------------");
            Console.WriteLine($"Title: {Title}");
            for (int i = 0; i < _grades.Length; ++i)
            {
                Grade grade = _grades[i];
                total += grade.GetPercentOfFinalGrade();
                Console.WriteLine($"Desc: {grade.Description}\nEarned: {grade.PercentEarned}\nPercent towards final grade: {grade.GetPercentOfFinalGrade()}\n");
            }

            Console.WriteLine($"Grade for the course: {total}");
            Console.WriteLine("-------------------------------------");
        }
        else
        {
            Console.WriteLine("Please add grades first.");
        }
    }
}
}

年级

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GradeCalculator
{
    class Grade
    {
        string _description;
        float _percentEarned;
        float _weight;

    public string Description
    {
        get
        {
            return _description;
        }
        set
        {
            _description = value;
        }
    }
    public float PercentEarned
    {
        get
        {
            return _percentEarned;
        }
        set
        {
            _percentEarned = value;

            if (value < 0.0f || value > 100.0f)
            {
                //_percentEarned = 0;
                Console.WriteLine("Percent earned was less than 0 or greater than 100 so value was set to 0.");
            }
            else
            {
                _percentEarned = value;
            }
        }
    }
    public float Weight
    {
        get
        {
            return _weight;
        }
        set
        {
            if (value < 0.0f || value > 100.0f)
            {
                //_weight = 0;
                Console.WriteLine("Weight was less than 0 or greater than 100 so value was set to 0.");
            }
            else
            {
                _weight = value;
            }
        }
    }

    public float GetPercentOfFinalGrade()
    {
        float result = (_percentEarned * _weight) / 100;
        return result;
    }
}
}

1 个答案:

答案 0 :(得分:0)

你所需要的只是在循环结束时清除控制台并使其保持在那里一段时间只是为了向用户显示输出你会做这样的事情 我添加了类似,3000意味着3000毫秒3秒我们将显示用户错误然后我们将清除输出并再次显示菜单,使用3秒或5秒(5000),你想要什么,然后清除输出

System.Threading.Thread.Sleep(3000);
                Console.Clear();


static void Main(string[] args)
        {
            Student currentStudent = null;

            bool running = true; // Logic 1
            string input = "";

            while (running)
            {
                // display menu

                Console.WriteLine("Main menu: ");
                Console.WriteLine("1. Create a student");
                Console.WriteLine("2. Add a course to the current student");
                Console.WriteLine("3. Remove a course from the current student");
                Console.WriteLine("4. Add grades for a course");
                Console.WriteLine("5. Display student info");
                Console.WriteLine("6. Display grades for a course");
                Console.WriteLine("7. Display all grades");
                Console.WriteLine("8. Exit");
                Console.Write("Enter a selection: (1 - 8): ");
                input = Console.ReadLine().ToLower();

                Console.WriteLine();
                // handle choices
                //------------------------------------------------------------------------
                switch (input)
                {
                    case "1":
                    case "create a student":
                        {
                            Console.Write("What is the students first name? ");
                            string firstName = Console.ReadLine();

                            Console.Write("What is the students last name? ");
                            string lastName = Console.ReadLine();
                            currentStudent = new Student(firstName, lastName);

                            Console.Write("How old is the student? ");
                            string studentAge = Console.ReadLine(); // Logic 2
                            int age;

                            while (!int.TryParse(studentAge, out age))
                            {
                                Console.Write("Please enter a number: ");
                                studentAge = Console.ReadLine();

                            }

                            currentStudent.Age = age;

                            Console.Write("What is the students address? ");
                            currentStudent.Address = Console.ReadLine();

                            Console.Write("What is the students email? ");
                            currentStudent.Email = Console.ReadLine();

                            Console.Write("What is the students phone number? ");
                            currentStudent.Phone = Console.ReadLine();
                        }
                        break;
                    case "2":
                    case "add a course to the current student":
                        {
                            if (currentStudent != null)
                            {
                                currentStudent.AddACourse();
                            }
                            else
                            {
                                Console.WriteLine("Please create a student first.");
                            }
                        }
                        break;
                    case "3":
                    case "remove a course from the current student":
                        {
                            if (currentStudent != null)
                            {
                                currentStudent.RemoveACourse();
                            }
                            else
                            {
                                Console.WriteLine("Please create a student first.");
                            }
                        }
                        break;
                    case "4":
                    case "add grades for a course":
                        {
                            if (currentStudent != null)
                            {
                                currentStudent.AddGradesForACourse();
                            }
                            else
                            {
                                Console.WriteLine("Please create a student first.");
                            }
                        }
                        break;
                    case "5":
                    case "display student info":
                        {
                            if (currentStudent != null)
                            {
                                currentStudent.DisplayInfo();
                            }
                            else
                            {
                                Console.WriteLine("Please create a student first.");
                            }
                        }
                        break;
                    case "6":
                    case "display grades for a course":
                        {
                            if (currentStudent != null)
                            {
                                currentStudent.DisplayGradesForACourse();
                            }
                            else
                            {
                                Console.WriteLine("Please create a student first.");
                            }
                        }
                        break;
                    case "7":
                    case "display all grades":
                        {
                            if (currentStudent != null)
                            {
                                currentStudent.DisplayAllGrades();
                            }
                            else
                            {
                                Console.WriteLine("Please create a student first.");
                            }
                        }
                        break;
                    case "8":
                    case "exit":
                        {
                            running = false; // Logic 6
                        }
                        break;
                    default:
                        return;
                }


                Console.WriteLine("You have chosen to exit");
                //Console.ReadKey();

                System.Threading.Thread.Sleep(3000);
                Console.Clear();
            }
        }