Arrays 如何将动态数组放入函数C#?
这是我的密码Arrays 如何将动态数组放入函数C#?,arrays,function,dynamic,Arrays,Function,Dynamic,这是我的密码 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;
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();
}
//
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间控制台EAPP1
{
公立班弱智学生
{
公共字符串Name=“1”;
公众智力;
public void PrintName()
{
控制台。写(“学生姓名”);
Console.ForegroundColor=ConsoleColor.Green;
控制台。写入(名称);
Console.ResetColor();
控制台。写入(“学生测试结果:”);
Console.ForegroundColor=ConsoleColor.Red;
控制台写入(IQ);
Console.ResetColor();
Console.WriteLine();
}
}
公共课程
{
公共不安全无效分类学生(弱智学生[]弱智)
{
智力;
字符串名;
int DELARTSNUMBER=延迟长度;
for(int i=0;i延迟[j+1].IQ |[j].IQ==延迟[j+1].IQ)和&(String.Compare(延迟[j].Name,0,延迟[j+1].Name,0,Math.Max(延迟[j].Name.Length,延迟[j+1].Name.Length))>0)
{
智商=延迟[j]。智商;
名称=延迟[j]。名称;
延迟[j]。IQ=延迟[j+1]。IQ;
延迟[j]。名称=延迟[j+1]。名称;
延迟[j+1]。IQ=IQ;
延迟[j+1]。名称=名称;
}
}
}
}
静态void Main(字符串[]参数)
{
不安全的
{
控制台。写入(“输入学生人数:”);
Console.WriteLine();
int delaytsnumber;//=Convert.ToInt32(Console.ReadLine());//Convert
Int32.TryParse(Console.ReadLine(),out delaystsnumber);//
如果(延迟数<1)
{
WriteLine(“您输入了零/非数值作为学生人数。程序执行完成”);
Console.ReadLine();
返回;
}
延迟学生[]延迟=新延迟学生[延迟人数];
for(int i=0;i延迟[j+1].IQ |[j].IQ==延迟[j+1].IQ)和&(String.Compare(延迟[j].Name,0,延迟[j+1].Name,0,Math.Max(延迟[j].Name.Length,延迟[j+1].Name.Length))>0)
{
tempreadit.IQ=延迟[j].IQ;
tempreadit.Name=延迟[j].Name;
延迟[j]。IQ=延迟[j+1]。IQ;
延迟[j]。名称=延迟[j+1]。名称;
延迟[j+1]。IQ=tempreadit.IQ;
延迟[j+1].Name=tempreadit.Name;
}
}
}
Console.WriteLine();
Console.WriteLine(“按分数排序,然后按名称排序”);
Console.WriteLine();
for(int i=0;i
我试着给巫师学生打电话,但无论我做什么,我总是会遇到错误。我试图使用不安全的,我添加了属性到项目,该属性允许我运行不安全的代码。
现在我的错误是CS0208
请帮忙解决这个问题。我很难理解视频后的指针和引用 我确信没有理由在这里使用不安全的代码。 看起来您的原始代码中缺少了
静态
public static void SortRetardedStudents(RetardedStudent[] Retards)
谢谢你的帮助!