-5

所以函数(命名为 InsertMark)的代码如下。你会如何调用这个函数来将 10 个人的分数输入到一个名为 iMarks 的数组中?

static void InsertMark(int [] piMarkArray, int piStuNum)
{
  int iMark;

  Console.Write("Enter mark for student " + piStuNum + ": ");
  iMark = Convert.ToInt32(Console.ReadLine());

  while (iMark < 0 || iMark > 100)
  {
    Console.Write("Not a percentage. Enter again: ");
    iMark = Convert.ToInt32(Console.ReadLine());
  }

  //update array element with this mark
  piMarkArray[piStuNum] = iMark;
  }

谢谢。

4

6 回答 6

1

只需将行移动到piMarkArray[piStuNum] = iMark;while循环中,使用索引,如果索引不小于数组长度,则退出循环。

 int index=0;
 while ((iMark < 0 || iMark > 100) && index < piMarkArray.Length) // exit the loop array is full
 {
    Console.Write("Not a percentage. Enter again: ");
    iMark = Convert.ToInt32(Console.ReadLine());
    piMarkArray[index++] = iMark; // Here marks are set
  }

  //update array element with this mark
于 2013-01-09T19:47:24.063 回答
0

在 main 函数中,您可以有一个代码:

 int iMarks[10];

for(int i = 0; i <10; i++ )
   InsertMark(iMarks, i)
于 2013-01-09T19:48:01.730 回答
0

你在找这样的东西吗?

for(int i=0; i<10; i++)
{
    InsertMark(iMarks, i);
}
于 2013-01-09T19:48:11.487 回答
0

您需要声明一个大小为 10: 的数组int[] iMarks = new int[10],然后在 for 循环中将数组和计数器值传递给函数。

        int[] iMarks = new int[10];
        for(int x = 0; x < 10; x++)
            InsertMark(iMarks, x);

这是完整的课程/工作示例:

static void Main(string[] args)
    {
        int[] iMarks = new int[10];
        for(int x = 0; x < 10; x++)
            InsertMark(iMarks, x);

        Console.Read();

    }

    static void InsertMark(int[] piMarkArray, int piStuNum)
    {
        int iMark;

        Console.Write("Enter mark for student " + piStuNum + ": ");
        iMark = Convert.ToInt32(Console.ReadLine());

        while(iMark < 0 || iMark > 100)
        {
            Console.Write("Not a percentage. Enter again: ");
            iMark = Convert.ToInt32(Console.ReadLine());
        }

        //update array element with this mark
        piMarkArray[piStuNum] = iMark;
    }
}
于 2013-01-09T19:52:21.690 回答
0

在这里,您创建将保存10标记并在循环中用您的方法填充它的数组:

int[] marks = new int[10];
for(int i = 0; i < marks.Length; i++)
    InsertMark(marks, i);
于 2013-01-09T19:47:47.077 回答
0

总是有多种方法可以编码任何东西,这也不例外。我在这里放的是一个惯用的 C# 示例来执行此操作。我能想到的至少有两种变体,哪个会更好,但这与最初的想法最接近。

首先,一个基础Student类:

class Student
{
   public int ID;
   public int Mark;
}

然后,一个提示输入标记的函数

int GetMark(int studentID)
{
    Console.Write("Enter mark for student " + studentID + ": ");
    int mark = Convert.ToInt32(Console.ReadLine());

    while (iMark < 0 || iMark > 100)
    {
        Console.Write("Not a percentage. Enter again: ");
        iMark = Convert.ToInt32(Console.ReadLine());
    }
    return mark;
}

最后,在你的主程序中,你有一个Student对象列表或数组被调用AllStudents,你可以这样做:

foreach (Student student in AllStudents)
{
    student.Mark = GetMark(student.ID); 
}

或者,如果您不使用类,您的循环可能是:

int[] marks = new int[10]; // Or whatever size it needs to be.
for (int i = 0; i < marks.Length; i++)
{
   marks[i] = GetMark(i);
}
于 2013-01-09T20:08:21.590 回答