如上所述,我正在尝试使用并行数组向正确的用户显示正确的分数,其中应显示学生的姓名及其得分低于40的分数。
目前我已经这样做了,但它没有将分数分配给正确的名称。
代码:
static void Main(string[] args)
{
//bool YesNo = Console.ReadKey;
// Empty Variable arrays declared for usage.
int[] classScores = new int[5];
string[] studentNames = new string[5];
object[] ClassANDStudent = new object[5];
//
// if (YesNo == true)
//Loop Method for user input for student name.
for (int i = 0; i < studentNames.Length; i++)
{
Console.WriteLine("Please enter a Student Name.");
studentNames[i] = Console.ReadLine();
Console.WriteLine("Please enter marks.");
classScores[i] = Convert.ToInt32(Console.ReadLine());
}
//
var Lessthan40Array = classScores.Where(OverallMark => OverallMark <= 40).ToArray();
Array.Sort(studentNames, classScores, 0, studentNames.Length);
Array.Sort(studentNames, Lessthan40Array, 0, Lessthan40Array.Length);
for (int i = 0; i < studentNames.Length; i++)
{
ClassANDStudent[i] = studentNames[i] + " " + classScores[i];
}
//
for (int i = 0; i < Lessthan40Array.Length; i++)
{
Console.WriteLine(studentNames[i] + " " + Lessthan40Array[i]);
// Console.WriteLine(" {0,-10}: {1}", studentNames[i], Lessthan40Array[i]);
}
Console.ReadKey();
}
答案 0 :(得分:0)
您真正需要做的就是遍历所有分数并输出学生信息(来自两个数组)以获得分数小于40的任何索引。上面代码的问题是,只要您排序其中一个阵列,它们不再同步。
在尝试从用户那里获取数字时,我通常做的第一件事就是创建一个辅助方法,它将进行一些验证(使用int.TryParse
)以确保它们实际输入一个整数。此方法将字符串作为向用户显示的提示,并一直提示它们,直到它们输入有效数字:
private static int GetIntFromUser(string prompt)
{
int input;
do
{
Console.Write(prompt);
} while (!int.TryParse(Console.ReadLine(), out input));
return input;
}
有了这个,这里的示例代码将提示用户他们想要输入多少学生,然后将每个学生的名称和分数收集到两个单独的数组中。然后它只使用一个计数器循环得分数组,并为任何得分低于40的人输出学生姓名和分数:
private static void Main()
{
int numStudents = GetIntFromUser("Enter the number of students: ");
int[] scores = new int[numStudents];
string[] names = new string[numStudents];
for (int i = 0; i < numStudents; i++)
{
Console.WriteLine("\nStudent #" + (i + 1));
Console.Write(" - Enter student's name: ");
names[i] = Console.ReadLine();
scores[i] = GetIntFromUser(" - Enter student's score: ");
}
Console.WriteLine("\nHere are the students who scored less than 40:");
for (int i = 0; i < numStudents; i++)
{
if (scores[i] < 40)
{
Console.WriteLine(" - " + names[i] + " scored " + scores[i]);
}
}
GetKeyFromUser("\nDone! Press any key to exit...");
}
<强>输出强>
答案 1 :(得分:0)
如果你想在写分数之前进行实际排序:
static void Main(string[] args)
{
int[] classScores = new int[]{30,50,25,39,62};
string[] studentNames = new string[]{"Jim","John","Mary","Peter","Sarah"};
Array.Sort(classScores, studentNames); // sort both according to scores
for (int i = 0; i < classScores.Length; i++)
{
if (classScores[i] < 40)
{
Console.WriteLine(classScores[i] + " " + studentNames[i]);
}
}
}
如果您有两个以上的数组,请创建一个索引数组0到length-1:
static void Main(string[] args)
{
int[] classScores = new int[]{30,50,25,39,62};
string[] firstNames = new string[]{"Jim","John","Mary","Peter","Sarah"};
string[] lastNames = new string[]{"Thorpe","Smith","Jones","Tork","Conner"};
int[] index = new int[]{0,1,2,3,4};
Array.Sort(classScores, index); // sort both according to scores
for (int i = 0; i < classScores.Length; i++)
{
if (classScores[i] < 40)
{
Console.WriteLine(classScores[i] + " " +
firstNames[index[i]] + " " + lastNames[index[i]]);
}
}
}