我正在尝试在 while 循环中获取一个 int 列表以在 c# 中存储用户输入



我得到了一个作业,我应该在其中收集学生号码,姓名和姓氏以及学生的测试分数并计算学生的平均值,然后将这些信息打印到控制台上窗户。现在,我正在努力创建一个WALE循环,该循环将接受用户输入列表中(因为用户将决定输入多少分数(。请注意,我是第一年的学生,也是编程的新生。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StudentRecord
{
class Program
{
    static void Main(string[] args)
    {
        Console.Write("Enter your name and surname: ");
        string NameSurname = Console.ReadLine();
        Console.Write("Enter  your student number:");
        string StudentNumber = Console.ReadLine();
        if (StudentNumber.Length > 8)
        {
            Console.WriteLine("Exceeded digits entered");
            StudentNumber = Console.ReadLine();
        }
        else if (StudentNumber.Length < 8)
        {
            Console.WriteLine("Insufficient digits entered");
            StudentNumber = Console.ReadLine();
        }
        if (StudentNumber.All(char.IsDigit) == true)
        {
            Console.WriteLine("Hello " + NameSurname + "Studentnumber: " + StudentNumber);

        }
        else
        {
            Console.WriteLine("has a non digit char");
        }

        List<int> Scores = new List<int>();

        while (true) {
            Console.WriteLine("Enter a test score: ");
            Scores = Convert.ToInt32(Console.ReadLine());
            break;
            };
   }
   }
   }

这可能是您要寻找的控制台应用程序。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp3
{
    struct Student
    {
        string number;
        string fullName; // both name and surname separated with space
        int score;
        public Student(string new_number, string new_fullName, int new_score)
        {
            number   = new_number;
            fullName = new_fullName;
            score    = new_score; 
        }
        public string toString()
        {
            return "Hello " + fullName + ", Studentnumber: " + number + " with Score: " + score.ToString();
        }
        public int getScore()
        {
            return score;
        }
    }
    class Program
    {
        private static List<Student> students = new List<Student>();
        private static void addNewStudent(Student student)
        {
            students.Add(student);
        }
        private static string getStudentNumber()
        {
            Console.Write("Enter your student number:");
            string studentNumber = Console.ReadLine();
            if (studentNumber.Length <= 8 && int.TryParse(studentNumber, out int n) == true)
            {
                return studentNumber;
            }
            else
            {
                Console.WriteLine("Insufficient digits entered");
                return getStudentNumber();
            }
        }
        private static string getStudentFullName()
        {
            Console.Write("Enter your name and surname: ");
            string fullName = Console.ReadLine();
            if(fullName.Contains(" "))
            {
                return fullName;
            }
            else
            {
                Console.WriteLine("You must enter both name and surname");
                return getStudentFullName();
            }
        }
        private static int getStudentScore()
        {
            Console.Write("Enter a test score: ");
            try
            {
                int score = Convert.ToInt32(Console.ReadLine());
                return score;
            }
            catch
            {
                Console.WriteLine("Score must be integer");
                return getStudentScore();
            }
        }
        static void getStudentInformation()
        {
            Console.WriteLine("Get new student information");
            string number = getStudentNumber();
            string fullName = getStudentFullName();
            int score = getStudentScore();
            Student newStudent = new Student(number, fullName, score);
            addNewStudent(newStudent);
            Console.WriteLine(newStudent.toString());

        }
        static void Main(string[] args)
        {
            while(true)
            {
                Console.Write("Enter A to Add new student, G to Get students list info and average score: ");
                string res = Console.ReadLine();
                if(res == "A")
                {
                    getStudentInformation();
                }
                else if (res == "G")
                {
                    if(students.Count == 0)
                    {
                        Console.WriteLine("Student Number in the List: 0");
                    }
                    else
                    {
                        int scoreSum = 0;
                        foreach(Student student in students)
                        {
                            scoreSum += student.getScore();
                        }
                        float averageScore = scoreSum / students.Count;
                        Console.WriteLine("Average score is: " + averageScore.ToString());
                    }
                }
                else
                {
                    Console.WriteLine("You have to type A or G");
                }
            }
        }
    }
}

我的机器的样本输出:

Enter A to Add new student, G to Get students list info and average score: asfsfnf
You have to type A or G
Enter A to Add new student, G to Get students list info and average score: A
Get new student information
Enter your student number:112
Enter your name and surname: bill gate
Enter a test score: 100
Hello bill gate, Studentnumber: 112 with Score: 100
Enter A to Add new student, G to Get students list info and average score: A
Get new student information
Enter your student number:steve job
Insufficient digits entered
Enter your student number:130
Enter your name and surname: steve job
Enter a test score: 0
Hello steve job, Studentnumber: 130 with Score: 0
Enter A to Add new student, G to Get students list info and average score: G
Average score is: 50
Enter A to Add new student, G to Get students list info and average score:

相关内容

  • 没有找到相关文章

最新更新