0

我已经获得了要更改的代码,我已经添加了一个包含初始学生信息的结构,它运行良好,代码如下:

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

namespace Program
{
class Student
{
    public struct student_data
    {
        public string forename;
        public string surname;
        public string prog_title;
        public string prog_code;
        public int id_number;
        public float averageGrade;

    }

    static void populateStruct(out student_data student, string fname, string surname,            string prog_title, string prog_code, int id_number)
    {
        student = new student_data();
        student.forename = fname;
        student.surname = surname;
        student.prog_title = prog_title;
        student.prog_code = prog_code;
        student.id_number = id_number;
        student.averageGrade = 0.0F;

    }

    static void Main(string[] args)
    {
        student_data[] students = new student_data[4];
        populateStruct(out students[0], "Mark", "Anders", "Comp", "CIS2117", 0);
        printStudent(students[0]);
        populateStruct(out students[1], "Tom", "Jones", "Comp", "CIS2117", 1);
        printStudent(students[1]);
        populateStruct(out students[2], "Tim", "Jones", "Comp", "CIS2117", 2);
        printStudent(students[2]);
        populateStruct(out students[3], "Tim", "Bones", "Comp", "CIS2117", 3);
        printStudent(students[3]);
    }
    void printAllStudent(student_data student)
    {
        for (int i = 0; i <= 3; i++)
        {
            Console.WriteLine(i);

        }
    }


    static void printStudent(student_data student)
    {
        Console.WriteLine("Name: " + student.forename + " " + student.surname); 
        Console.WriteLine("Id: " + student.id_number);
        Console.WriteLine("AV grade: " + student.averageGrade);
        Console.WriteLine("Course Title: " + student.prog_title);
        Console.WriteLine("Course Code: " + student.prog_code);


    }
}

}

但是我的任务是添加另一个结构来保存我已经完成的 module_data,我还创建了一个新方法来填充 module_data 数组。但是当我运行程序时,只出现一个错误,什么也没发生?

它的目的是在控制台屏幕中写入数组中的所有元素,但不会构建并产生此错误:

'错误 1 ​​找不到类型或命名空间名称'module_data'(您是否缺少 using 指令或程序集引用?)'

代码如下:

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

namespace Program
{
class Student
{
    public struct student_data
    {
        public string forename;
        public string surname;
        public string prog_title;
        public string prog_code;
        public int id_number;
        public float averageGrade;

    }

    public struct module_data
    {
        public string module_code;
        public string module_title;
        public int module_mark;
    }

    static void populateStruct(out student_data student, string fname, string surname,    string prog_title, string prog_code, int id_number)
    {
        student = new student_data();
        student.forename = fname;
        student.surname = surname;
        student.prog_title = prog_title;
        student.prog_code = prog_code;
        student.id_number = id_number;
        student.averageGrade = 0.0F;

    }

    static void populateModule(out module_data module, string mcode, string mname, int   (score)
    {
        module = new module_data();
        module.module_code = mcode;
        module.module_title = mname;
        module.module_mark = score;
    }

    static void Main(string[] args)
    {
        {
            student_data[] students = new student_data[5];
            populateStruct(out students[0], "Mark", "Anderson", "Comp", "CIS2117", 0);
            printStudent(students[0]);
            populateStruct(out students[1], "Tom", "Jones", "Comp", "CIS2117", 1);
            printStudent(students[1]);
            populateStruct(out students[2], "Tim", "Jones", "Comp", "CIS2117", 2);
            printStudent(students[2]);
            populateStruct(out students[3], "Tim", "Bones", "Comp", "CIS2117", 3);
            printStudent(students[3]);
        }
        {
            module_data[] modules = new module_data[4];
            populateStruct(out modules[0], "7", "App Dev", "56", 0);
            printStudent(modules[0]);
            populateStruct(out modules[1], "7", "App Dev", "56", 1);
            printStudent(module[1]);
            populateStruct(out modules[2], "7", "App Dev", "56", 2);
            printStudent(modules[2]);
            populateStruct(out modules[3], "7", "App Dev", "56", 3);
            printStudent(modules[3]);
            Console.ReadKey();
        }
    }

    void printAllStudent(student_data student)
    {
        for (int i = 0; i <= 3; i++)
        {
            Console.WriteLine(i);

        }
    }


    static void printStudent(student_data student)
    {
        Console.WriteLine("Name: " + student.forename + " " + student.surname);
        Console.WriteLine("Id: " + student.id_number);
        Console.WriteLine("AV grade: " + student.averageGrade);
        Console.WriteLine("Course Title: " + student.prog_title);
        Console.WriteLine("Course Code: " + student.prog_code);
        Console.WriteLine("Module Code: " + modules.mcode);
        Console.WriteLine("Module Name: " + modules.mname);
        Console.WriteLine("Score: " + modules.score);

    }
}

}

事实上,我不确定我哪里出错了,任何帮助或建议将不胜感激。

4

1 回答 1

1

我不确定 amodule是否是 astudent拥有的东西。这个答案让他们独立。尽管代码需要额外的工作,但这至少会清理您的代码并让您开始。此处未遵循命名约定和其他标准。

我建议你先写一小部分功能,然后让它工作,然后再继续做其他事情。您的代码中有许多无法编译的错误。在错误发生时修复错误并测试小块将帮助您找出问题所在。

下面的链接是解释代码中的注释

在类和结构之间进行选择

默认构造函数

如何:使用对象初始化器初始化对象

显示它实现的数组类IEnumerable

class Program
{
    //Use a class instead of a struct to store your data in most cases... see link
    public class Student
    {
        public string forename { get; set; }
        public string surname { get; set; }
        public string prog_title { get; set; }
        public string prog_code { get; set; }
        public int id_number { get; set; }
        public float averageGrade { get; set; }

        //I ommited the defualt {} constructor so this is your only choice to create a new class
        //you may want to choose to put it back in for more flexibility...see link
        public Student(string fname, string surname, string prog_title, string prog_code, int id_number)
        {
            forename = fname;
            this.surname = surname;
            this.prog_title = prog_title;
            this.prog_code = prog_code;
            this.id_number = id_number;
            averageGrade = 0.0F;
        }
    }

    public class module_data
    {
        public string module_code { get; set; }
        public string module_title { get; set; }
        public int module_mark { get; set; }

        public module_data(string mcode, string mname, int score)
        {
            module_code = mcode;
            module_title = mname;
            module_mark = score;
        }
    }

    static void Main(string[] args)
    {
        //I'm initializing the array using object initialization syntax ... see link
        Student[] students = new Student[4]
            {
                new Student( "Mark", "Anderson", "Comp", "CIS2117", 0),
                 new Student( "Tom", "Jones", "Comp", "CIS2117", 1),
                 new Student ("Tim", "Jones", "Comp", "CIS2117", 2),
                 new Student( "Tim", "Bones", "Comp", "CIS2117", 3)
            };

        module_data[] modules = new module_data[4]
        {
            new module_data( "7", "App Dev", 0),
            new module_data( "7", "App Dev", 1),
            new module_data("7", "App Dev", 2),
            new module_data("7", "App Dev", 3)
        };

        printAllStudent(students);
        Console.ReadKey();
    }

    //Because an array implements IEnumerable you should use a foreach loop instead of a for loop
    static void printAllStudent(Student[] students)
    {
        foreach (Student s in students)
        {
            printStudent(s);
        }
    }

    //You could pass a null in here and this would have a run-time error.
    //It would be safer to check if student!=null here first (but I left it for you)
    static void printStudent(Student student)
    {
        Console.WriteLine("Name: " + student.forename + " " + student.surname);
        Console.WriteLine("Id: " + student.id_number);
        Console.WriteLine("AV grade: " + student.averageGrade);
        Console.WriteLine("Course Title: " + student.prog_title);
        Console.WriteLine("Course Code: " + student.prog_code);
    }

    static void printModule(module_data m)
    {
        Console.WriteLine("Module Code: " + m.module_code);
        Console.WriteLine("Module Name: " + m.module_title);
        Console.WriteLine("Score: " + m.module_mark);

    }
}
于 2013-10-16T20:01:38.240 回答