我将它排序后打印成绩数组,但显示错误的等级名称。
class Program
{
static void Main(string[] args)
{
int namelength;
string names;
double grade, max, min, testgradeaverage;
int nameindex = 0;
int gradeindex = 0;
Console.WriteLine("Please enter the amount of names you wish to enter: ");
namelength = Convert.ToInt32(Console.ReadLine());
string[] name = new string[namelength];
double[] testgrades = new double[namelength];
for (int i = 0; i < testgrades.Length; i++)
{
Console.WriteLine("Please enter the names of each student");
names = Convert.ToString(Console.ReadLine());
name[i] += names;
nameindex = i;
}
for (int i = 0; i < name.Length; i++)
{
Console.WriteLine("Please enter the final grade for " + name[i]);
grade = Convert.ToDouble(Console.ReadLine());
testgrades[i] += grade;
gradeindex = i;
}
max = testgrades.Max();
min = testgrades.Min();
testgradeaverage = testgrades.Average();
Console.WriteLine("The highest grade is: " + max);
Console.WriteLine("The Lowest Grade is:" + min);
Console.WriteLine("The class average is: " + testgradeaverage);
for (int k = 0; k < namelength; k++)
{
sorted(ref testgrades);
Console.WriteLine(name[nameindex] + testgrades[k]);
}
Console.ReadLine();
}
public static void sorted(ref double [] testgrades)
{
double temp = 0;
for (int write = 0; write < testgrades .Length; write++)
{
for (int sort = 0; sort < testgrades.Length - 1; sort++)
{
if (testgrades [sort] <= testgrades [sort + 1])
{
temp = testgrades[sort + 1];
testgrades [sort + 1] = testgrades [sort];
testgrades [sort] = temp;
}
}
}
}
答案 0 :(得分:3)
arrays
超载,接受两个Array.Sort(testgrades, names);
;一个用于钥匙,一个用于物品。两者的项目都根据keys数组排序:
JTextField
如果您无法从此重载中获得结果,则可以创建自定义比较器并将其作为第三个参数传递。
答案 1 :(得分:0)
创建自定义类型,例如:
public class NameGrade
{
public string Name;
public double Grade;
}
然后只使用该类型的一个数组。用数据填充,然后对元素进行排序。您可以使用扩展方法(using System.Linq
)代替使用自己的排序方法:NameGrade[] sorted = myArray.OrderBy (x => x.Grade).ToArray ();
修改强>
看起来像Array.Sort (keys, values)
做的工作。但是,最好使用自定义类型(更好的代码可读性)。
答案 2 :(得分:0)
简短的回答改变了这一点:
sorted(testgrades, names);
for (int k = 0; k < namelength; k++)
{
Console.WriteLine(name[k] + testgrades[k]);
}
对此。由于每个数组中的项目数相同:
sorted
您的public static void sorted(double[] testgrades, string[] names)
{
double temp = 0;
string nameTemp = "";
for (int write = 0; write < testgrades.Length; write++)
{
for (int sort = 0; sort < testgrades.Length - 1; sort++)
{
if (testgrades[sort] <= testgrades[sort + 1])
{
temp = testgrades[sort + 1];
testgrades[sort + 1] = testgrades[sort];
testgrades[sort] = temp;
// If you move the grade to a different index, you need to move the corresponding grade
// as well
nameTemp = names[sort + 1];
names[sort + 1] = names[sort];
names[sort] = nameTemp;
}
}
}
}
方法:
testGrades
答案很长:
一些事情:
sorted
数组。 C#将通过引用传递此数组,因为数组不是值类型。OrderBy
方法,因为你一遍又一遍地对相同的值进行排序。OrderByDescending
,它将按升序排序并使用for (int i = 0; i < testgrades.Length; i++)
{
Console.WriteLine("Please enter the names of each student");
names = Convert.ToString(Console.ReadLine());
name[i] += names;
nameindex = i;
}
,它将按降序排序。您有以下代码:
nameindex
在循环后看变量i
将具有最后{{1}}值。因此,当您稍后编写名称时,在排序后,您将始终显示相同的名称(输入的姓氏)。