首页 > 解决方案 > c#中的for循环错误

问题描述

我正在做一个包含一个类的作业,程序必须采用名字、姓氏和 GPA,但 id 是从循环中生成的。必须创建一个学生结构和两个函数,一个用于获取对应于 id 的 GPA,另一个用于打印学生列表和应该组合的主干。每个学生的初始 GPA 为 0,直到被函数更新。问题是更新 GPA 的功能在完成所有学生后出现错误,即使没有 id 0,它也开始重复 id 0。这是我的代码:

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

namespace Assignment_1
{
    class Program
    {
        public struct student {
            public int id;
            public String fName, lName;
            public float gpa;
        };

        public static void printList(student[] std) {
            for (int i = 0; i < std.Length; i++) {
                Console.WriteLine("Student #" + (i + 1) + " :");
                Console.WriteLine("First Name: " + std[i].fName);
                Console.WriteLine("Last Name: " + std[i].lName);
                Console.WriteLine("ID: " + std[i].id);
                Console.WriteLine("GPA: " + std[i].gpa);
                Console.WriteLine();
            }
        }

        public static void gpa(student[] std) {
            for (int i = 0; i < std.Length; i++) {
                Console.WriteLine("Enter GPA for ID: " + std[i].id);

                do
                {
                    std[i].gpa = Int32.Parse(Console.ReadLine());
                } while (std[i].gpa < 0 || std[i].gpa > 4);

                Console.WriteLine();
            }
        }

        static void Main(string[] args)
        {
            student[] std = new student[30];
            for (int n = 0; n < 5; n++) {
                Console.WriteLine("Enter the first name of student " + (n + 1));
                std[n].fName = Console.ReadLine();
                Console.WriteLine("Enter the last name of student " + (n + 1));
                std[n].lName = Console.ReadLine();
                Console.WriteLine();
                std[n].id = n + 1;
                std[n].gpa = 0;
            }
            gpa(std);
            printList(std);
        }
    }
}

标签: c#

解决方案


尝试使用 List 而不是数组。当您为 30 个学生声明一个结构数组时,即使您没有设置值,它们也会在内存上分配。使用任何 ICollections 代替,例如列表。

集合将仅存储通过 Add() 方法添加的实体。

        public struct student
        {
            public int id;
            public String fName, lName;
            public float gpa;
        };

        public static void printList(List<student> stds)
        {
            for (int i = 0; i < stds.Count; i++)
            {
                Console.WriteLine("Student #" + (i + 1) + " :");
                Console.WriteLine("First Name: " + stds[i].fName);
                Console.WriteLine("Last Name: " + stds[i].lName);
                Console.WriteLine("ID: " + stds[i].id);
                Console.WriteLine("GPA: " + stds[i].gpa);
                Console.WriteLine();
            }
        }

        public static void gpa(List<student> stds)
        {
            //Instead of length, Use count in a collection
            for (int i = 0; i < stds.Count; i++)
            {
                var item = stds[i];
                Console.WriteLine("Enter GPA for ID: " + item.id);
                item.gpa = Int32.Parse(Console.ReadLine());
            }
        }

        public static void Main(string[] args)
        {
            //Instead of an array, Declare studets as a list (collection)
            List<student> stds = new List<student>();

            for (int n = 0; n < 5; n++)
            {
                //Create a student everytime you loop
                var std = new student();
                Console.WriteLine("Enter the first name of student " + (n + 1));
                std.fName = Console.ReadLine();
                Console.WriteLine("Enter the last name of student " + (n + 1));
                std.lName = Console.ReadLine();
                Console.WriteLine();
                std.id = n + 1;
                std.gpa = 0;

                //And add it to our list
                stds.Add(std);
            }
            gpa(stds);
            printList(stds);
        }

输出:

在此处输入图像描述


推荐阅读