添加附加结构以保存数据,然后在控制台窗口中打印出来

时间:2013-10-16 19:04:10

标签: c#

我已经给了这个代码进行更改,我已经添加了一个包含初始学生信息的结构,并且运行正常,代码如下:

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


    }
}

}

但我的任务是添加另一个Struct来保存我已经完成的module_data,我还创建了一个新方法来填充module_data数组。但是,当我运行该程序时,只显示一个错误,没有任何反应?

这意味着WriteLine控制台屏幕中阵列中的所有元素,但不会构建并产生此错误:

'错误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);

    }
}

}

总而言之,不确定我哪里出错,任何帮助或建议都会受到赞赏。

1 个答案:

答案 0 :(得分:1)

我不确定module是否属于student。这个答案让他们独立。虽然代码需要额外的工作,但这至少可以清理代码并让您入门。这里没有遵循命名约定和其他标准。

我建议你写一小段功能,然后再继续工作。您的代码中有许多错误无法编译。修复错误并测试小块将帮助您找出问题所在。

以下链接用于解释代码中的注释

Choosing Between Class and Struct

Default constructors

How to: Initialize Objects by Using an Object Initializer

Array Class显示它实现了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);

    }
}