关于数组和方法的问题 [英] Question on array and methods

查看:77
本文介绍了关于数组和方法的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public static void Main(string[] args)
{
    //change the number of arrays to 10!
    string[] studentNames = new string[3];
    int[] finalScores = new int[3];

    int minIndex, minGoes, temp;

    for (int i = 0; i < studentNames.Length; i++)
    {
        Console.Write("Enter student name " + ": ");
        studentNames[i] = Convert.ToString(Console.ReadLine());
        Console.Write("Enter student score " + ": ");
        finalScores[i] = Convert.ToInt32(Console.ReadLine());
    }

    minGoes = 0;
    minIndex = 0;

    for (int j = 1; j < finalScores.Length; j++)
    {
        if (finalScores[j] > finalScores[minIndex])
        {
            minIndex = j;
        }
        temp = finalScores[minIndex];
        finalScores[minIndex] = finalScores[minGoes];
        finalScores[minGoes] = temp;

        for (int i = 0; i < finalScores.Length - 1; i++)
        {
            minIndex = i;
            for (int j = i + 1; j < finalScores.Length; j++)
            {
                if (finalScores[j] > finalScores[minIndex])
                {
                    minIndex = j;
                }
                temp = finalScores[minIndex];
                finalScores[minIndex] = finalScores[i];
                finalScores[i] = temp;
            }
            Console.WriteLine("Student Scores: ");
            for (int i = 0; i < finalScores.Length; i++)
            {
                Console.Write(finalScores[i] + "\n ");
            }





我的尝试:



到目前为止,我已将其显示为从最高到最低的测试分数。我无法弄清楚如何从最高到最低的考试成绩获得正确的名字?



我怎么能合并一些方法?



What I have tried:

So far I have it showing the test scores in highest to lowest. I cant figure out how to get names to be right with the highest to lowest test scores ?

Also how could I incorporate a few methods ?

推荐答案

两个数组都没有链接办法。如果您使用学生对象列表(包含名称和分数的属性),然后在名称属性上对列表进行排序,您会好运。



其他提示:



0)永远不要将.Net中的字符串与+运算符连接起来。改为使用 string.Concat()



1)没有必要使用 Console.Write(... \ n)何时可以使用 Console.WriteLine(...)



BTW,包括使用 Console 对象的问题几乎总是与家庭作业有关。另一个线索是,没有真正的程序员会考虑使用两个数组而不是一个对象列表。如果你的导师强制要求两个阵列范例,他就是个白痴。
The two arrays are not linked in any way. You would have better luck if you used a List of student objects (that contained properties for the name and the score), and then sort the list on the name property.

Other tips:

0) Never concatenate strings in .Net with the "+" operator. Use string.Concat() instead.

1) There's no need to use Console.Write("...\n") when you can use Console.WriteLine("...").

BTW, questions that included the use of the Console object are pretty much always related to homework. Another clue is that no real programmer in his right mind would consider using two arrays instead of a list of objects. If your instructor is mandating the two array paradigm, he's an idiot.


这篇关于关于数组和方法的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆