Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c中的循环错误#_C# - Fatal编程技术网

C# c中的循环错误#

C# c中的循环错误#,c#,C#,我正在做一个由类组成的赋值,程序必须取名字、姓氏和GPA,但id是从循环生成的。必须创建一个学生结构和两个函数,一个用于获取与id对应的GPA,另一个用于打印学生列表和应合并的主目录。在函数更新之前,每个学生的初始GPA为0。问题是,更新GPA的函数在完成所有学生后出现问题,它开始重复id 0,即使没有id 0。这是我的密码: using System; using System.Collections.Generic; using System.Linq; using System.Text;

我正在做一个由类组成的赋值,程序必须取名字、姓氏和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);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间分配\u 1
{
班级计划
{
公共结构学生{
公共int id;
公共字符串fName,lName;
公共浮动gpa;
};
公共静态无效打印列表(学生[]标准){
对于(int i=0;i<标准长度;i++){
Console.WriteLine(“学生”#“+(i+1)+“:”;
Console.WriteLine(“名字:+std[i].fName);
Console.WriteLine(“姓氏:”+std[i].lName);
Console.WriteLine(“ID:+std[i].ID”);
Console.WriteLine(“GPA:+std[i].GPA”);
Console.WriteLine();
}
}
公共静态无效gpa(学生[]标准){
对于(int i=0;i<标准长度;i++){
WriteLine(“输入ID的GPA:+std[i].ID”);
做
{
std[i].gpa=Int32.Parse(Console.ReadLine());
}而(std[i].gpa<0 | | std[i].gpa>4);
Console.WriteLine();
}
}
静态void Main(字符串[]参数)
{
学生[]标准=新生[30];
对于(int n=0;n<5;n++){
Console.WriteLine(“输入学生的名字”+(n+1));
std[n].fName=Console.ReadLine();
Console.WriteLine(“输入学生的姓氏”+(n+1));
std[n].lName=Console.ReadLine();
Console.WriteLine();
标准[n].id=n+1;
标准[n].gpa=0;
}
gpa(标准);
打印列表(std);
}
}
}

我会将您的主函数更改为这样的函数,它只会为您需要的确切学生人数定义一个数组,请注意添加了“int numStudents=5;”以及在for循环的std和break条件声明中使用numStudents变量:

    static void Main(string[] args)
    {
        int numStudents = 5;
        student[] std = new student[numStudents];
        for (int n = 0; n < numStudents ; 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);
    }
static void Main(字符串[]args)
{
int numStudents=5;
学生[]std=新学生[numStudents];
对于(int n=0;n
尝试使用列表而不是数组。当您为30个学生声明一个结构数组时,即使您没有设置值,也会在内存中分配这些结构。 使用任何ICollections,例如列表

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

公共结构学生
{
公共int id;
公共字符串fName,lName;
公共浮动gpa;
};
公共静态无效打印列表(列表STD)
{
for(int i=0;i
输出:


如果只有5名学生,为什么要声明大小为30的数组?现在是学习如何使用调试器的时候了。如果你一步一步地检查代码和变量,你可以看到它没有达到你期望的效果,并发现原因。可能是因为student是一个结构体。你收到编译器警告了吗?在填充之前,将其设为类并“新建”。是的,您将看到ID为0,因为您正在声明一个包含30个学生的数组,并且只填充其中的5个学生。剩下的25的ID是0,因为整数的默认值是0,而结构本身并不像类那样默认为null。@HansKesting他仍然会有问题
        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);
        }