微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用类和构造函数读取名称和值

如何解决使用类和构造函数读取名称和值

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

例如学校有 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())
            }


        }

       

       
    }
}


解决方法

  1. 我会将您的 Student 类更改为使用自动属性

  2. 使用 List<T> 来存储创建的每个新学生

  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();
    }
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?