【问题标题】:How to put dynamic array in function C#?如何将动态数组放入函数C#中?
【发布时间】:2017-12-14 19:20:55
【问题描述】:

这是我的代码

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

namespace ConsoleApp1
{
    public class RetardedStudent
    {
        public String Name = "1";
        public int IQ;
        public void PrintName()
        {
            Console.Write("Student Name ");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write(Name);
            Console.ResetColor();
            Console.Write(" Student Test Result: ");
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write(IQ);
            Console.ResetColor();
            Console.WriteLine();
        }
    }
    public class Program
    {
        public unsafe void SortRetardedStudents(RetardedStudent[] Retards)
        {
            int IQ;
            string Name;
            int RetardsNumber = Retards.Length;
            for (int i = 0; i < RetardsNumber; i++)
            {
                for (int j = 0; j < RetardsNumber - i - 1; j++)
                {
                    if (Retards[j].IQ > Retards[j + 1].IQ || (Retards[j].IQ == Retards[j + 1].IQ) && (String.Compare(Retards[j].Name, 0, Retards[j + 1].Name, 0, Math.Max(Retards[j].Name.Length, Retards[j + 1].Name.Length)) > 0))
                    {
                        IQ = Retards[j].IQ;
                        Name = Retards[j].Name;
                        Retards[j].IQ = Retards[j + 1].IQ;
                        Retards[j].Name = Retards[j + 1].Name;
                        Retards[j + 1].IQ = IQ;
                        Retards[j + 1].Name = Name;
                    }

                }
            }
        }
        static void Main(string[] args)
        {
            unsafe
            {
                Console.Write("Input number of students:");
                Console.WriteLine();
                int RetardsNumber;// = Convert.ToInt32(Console.ReadLine());//Convert не прокатывает если ввести не число
                Int32.TryParse(Console.ReadLine(), out RetardsNumber);//на случай ввода не числа не будет вылета - будет словно ввели 0
                if (RetardsNumber < 1)
                {
                    Console.WriteLine("You entered zero/nonnumeric values as number of students. Programm execution is finished");
                    Console.ReadLine();
                    return;
                }
                RetardedStudent[] Retards = new RetardedStudent[RetardsNumber];
                for (int i = 0; i < RetardsNumber; i++)//fill students and their scores
                {
                    Retards[i] = new RetardedStudent();
                    Console.Write("Input Name and score for student # {0} ", i + 1);
                    String StudentNameAndScore = Console.ReadLine();
                    int SpaceIndex = StudentNameAndScore.LastIndexOf(" ");
                    if (SpaceIndex != -1)
                    {
                        Retards[i].Name = StudentNameAndScore.Remove(SpaceIndex, StudentNameAndScore.Length - SpaceIndex);
                        Int32.TryParse(StudentNameAndScore.Remove(0, SpaceIndex + 1), out Retards[i].IQ);
                    }
                    else
                    {
                        Retards[i].Name = StudentNameAndScore;
                        Retards[i].IQ = 0;
                    }
                }
                if (RetardsNumber == 1)
                {
                    Console.WriteLine("You entered 1 student " + Retards[0].Name + " with iq score test result: " + Retards[0].IQ + " Programm execution is finished because no need in sorting");
                    Console.ReadLine();
                    return;
                }
                RetardedStudent tempRetard = new RetardedStudent();
                SortRetardedStudents(&Retards);

                for (int i = 0; i < RetardsNumber; i++)
                {
                    for (int j = 0; j < RetardsNumber - i - 1; j++)
                    {
                        if (Retards[j].IQ > Retards[j + 1].IQ || (Retards[j].IQ == Retards[j + 1].IQ) && (String.Compare(Retards[j].Name, 0, Retards[j + 1].Name, 0, Math.Max(Retards[j].Name.Length, Retards[j + 1].Name.Length)) > 0))
                        {
                            tempRetard.IQ = Retards[j].IQ;
                            tempRetard.Name = Retards[j].Name;
                            Retards[j].IQ = Retards[j + 1].IQ;
                            Retards[j].Name = Retards[j + 1].Name;
                            Retards[j + 1].IQ = tempRetard.IQ;
                            Retards[j + 1].Name = tempRetard.Name;
                        }

                    }
                }
                Console.WriteLine();
                Console.WriteLine("sorted by score then by Name");
                Console.WriteLine();
                for (int i = 0; i < RetardsNumber; i++)
                {
                    Console.Write("# {0} ", i + 1);
                    Retards[i].PrintName();
                }
                //  Console.WriteLine("X format: {0:X}",99999);
                Console.ReadLine();
            }
            //
        }
    }
}

我尝试调用 SortRetardedStudents,但无论我做什么,我总是遇到错误。我尝试使用不安全的,我在项目中添加了允许我运行不安全代码的属性。 现在我的错误是 CS0208

请帮助解决问题。在视频之后,我很难理解指针和参考。

【问题讨论】:

    标签: arrays function dynamic


    【解决方案1】:

    我确信这里没有理由使用不安全的代码。 看起来您在原始代码中错过了static

    public static void SortRetardedStudents(RetardedStudent[] Retards)
    

    【讨论】:

    • 感谢您的帮助!
    猜你喜欢
    • 2014-06-20
    • 1970-01-01
    • 2021-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多