【问题标题】:Read names and values with class and constructor [closed]使用类和构造函数读取名称和值 [关闭]
【发布时间】:2021-04-01 16:00:26
【问题描述】:

我想知道学校有多少学生,并使用班级和构造函数一一读取这些学生的姓名和年级。然后,显示学校的成绩表。

例如学校有 5 个学生,我输入

  • “约翰”“85”

  • “迈克尔”“59”

  • “罗伯特”“64”

  • “简”“100”

  • “托尼”“42”

那么我希望程序会显示这个表格

  • 0-39:
  • 40-69:***
  • 70-89:*
  • 90-100:*

这是小代码和设计。 (这不是考试问题或作业,我是 C# 的新手,只是尝试解决一些问题。

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

namespace csharcpconsole
{
   
    class Program
    {
        class Student
        {
            public string name;
            public int grade;

            public Student(string studentname, int studentgrade)
            {
                name = studentname;
                grade = studentgrade;
            }

        }

        static void Main ()
        {
            Console.WriteLine("How many student you will add?");
            int studentcount = int.Parse(Console.ReadLine());

            for(int i=0;i<studentcount;i++)
            {
                Console.WriteLine($"Please enter the {i + 1}'th student name.");
               Student  st = new Student (Console.ReadLine())

                Console.WriteLine($"Please enter the {i + 1}'th student name and grade.");
                Student st = new Student int.Parse(Console.ReadLine())
            }


        }

       

       
    }
}


【问题讨论】:

  • 这到底是什么问题?你期望有什么输出?
  • 您的代码有效吗?如果没有,什么不起作用?你有什么问题吗?如果是这样,是什么?看起来你写了前几行并放弃了。考虑将输入的数据保存在List&lt;Student&gt; 中。还可以考虑使用int.TryParse 而不是int.Parse (这样您的程序就不会因无效条目而崩溃。此外,与其先询问有多少学生,不如继续询问,直到有人输入一个空字符串作为学生姓名

标签: c# oop io


【解决方案1】:
  1. 我会更改您的学生类以使用自动属性

  2. 使用List&lt;T&gt; 存储创建的每个新学生

  3. 打印到控制台并使用Linq 获取年级范围内的学生人数

    public class Student
    {
        public string Name { get; set;} //auto property
        public int Grade { get; set; } //auto property
    }
    
    static void Main ()
    {
        Console.WriteLine("How many student will you add?");
        string consoleInput = Console.ReadLine();
        //int for out parameter of int.TryParse()
        int outInt = default(int); //Or in C# 9 - int outInt = default;
        int studentcount = Int32.TryParse(consoleInput, out outInt) ? 
                             outInt : 0; // Or throw an exception??
        //initialize a List<T> of Student type
        List<Student> students = new List<Student>(studentcount);
    
        for(int i=0;i<studentcount;i++)
        {               
           Console.WriteLine($"Please enter the {i + 1}'th student name.");
           Student st = new Student { Name = Console.ReadLine() };
    
           Console.WriteLine($"Please enter the {i + 1}'th student name and grade.");
           //You don't need to create another Student, use the one from above
           //re-use the string from above
           consoleInput = Console.ReadLine();
           st.Grade = Int32.TryParse(consoleInput, out outInt) ? 
                          outInt : 0; //Or throw exception??
    
           //Add your student to the list
           students.Add(st);
        }
    
        //Print the result to the console
        if(students.Count > 0)
        {
            //Get students 0-39
            int number = students.Where(z => z.Grade < 40).Count();
            Console.WriteLine("0-39: " + ReturnAsteriks(number));
            //students 40-69
            number = students.Where(z => z.Grade >= 40 && z.Grade < 70).Count();
            Console.WriteLine("40-69: " + ReturnAsteriks(number));
            //students 70-89
            number = students.Where(z => z.Grade >= 70 && z.Grade < 90).Count();
            Console.WriteLine("70-89: " + ReturnAsteriks(number));
            //students 90-100
            number = students.Where(z => z.Grade >= 90).Count();
            Console.WriteLine("90-100: " + ReturnAsteriks(number));
        }
    }
    
    //Private method to build the number of asteriks
    //Takes an int and returns a string of asteriks
    private string ReturnAsteriks(int numOfAsteriks)
    {
        StringBuilder sb = new StringBuilder("");
        for(int z = 0; z < numOfAsteriks; z++)
        {
            sb.Append("*");
        }
    
        return sb.ToString();
    }
    

【讨论】:

  • 非常感谢!我还没有达到 C# 课程中的 Linq 部分,但我会研究你的代码。
  • @BerkeTabak 不客气。如果您有任何问题,请告诉我。希望这会有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 2018-08-10
  • 1970-01-01
  • 2021-05-27
  • 2021-11-11
相关资源
最近更新 更多