繁体   English   中英

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

[英]I'm trying to get a int list in a while loop to store user input in c#

我得到了一个作业,我应该收集学生的学号、姓名和考试成绩并计算学生的平均分,然后将该信息打印到控制台窗口。 现在我正在努力创建一个将接受用户输入到列表中的 while 循环(因为用户将决定输入多少分数)。 请注意,我是一年级学生,并且是编程新手。

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:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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