c# - 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);
}
}
}
解决方案
尝试使用 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);
}
输出:
推荐阅读
- discord.py - Discord.py AttributeError:“命令”对象没有属性“冷却”
- java - 集合可以作为 org.quartz.JobDataMap 内部的参数传递吗?
- html - 文本对齐问题 - H4 内容显示在图像右侧
- amazon-web-services - 找不到数据库驱动程序:用于 RDS postgres 连接-Liquibase 的 org.postgresql.Driver
- c# - 如何使用复选框将 datagridview 导出到 Excel
- python - ISSUE ON NOT NULL 约束失败:accounts_user.course_id
- node.js - Sequelize hasMany 关联不起作用
- wpf - Appium 找不到折叠的元素
- ros - rosbag play(和 rqt_bag)和 rosbag.Bag 的 read_messages() 使用的时间戳不同
- c# - 需要了解如何在 azure vm lunix 中运行简单的 shell 脚本