用户输入2d多维数组

时间:2018-03-17 18:25:36

标签: c# arrays multidimensional-array console-application

简单的控制台应用程序,将用户输入作为学生和分数进入多维数组。无法从循环中获取输入的分数到阵列中。我需要一个嵌套循环吗?

     Console.WriteLine("How many students will you be grading?");
            string totalStudents = Console.ReadLine();
            int studentSum = Convert.ToInt32(totalStudents);
            Console.WriteLine(string.Empty);
            Console.WriteLine("How many test scores will you enter for each student?");
            string totalScores = Console.ReadLine();
            int scoreSum = Convert.ToInt32(totalScores);

            int[,] studentScores = new int[studentSum, scoreSum];
            Console.WriteLine(string.Empty);


        for (int scoreIndex = 0; scoreIndex < scoreSum; scoreIndex++)
        {
            Console.WriteLine("Enter score");
            var score = Console.ReadLine();
            Console.WriteLine(string.Empty);
            int parsedScore = 0;
            success = int.TryParse(score, out parsedScore);

            studentScores[scoreSum] = parsedScore;

        }

1 个答案:

答案 0 :(得分:1)

你需要另外一个循环,比如

    for (int scoreIndex = 0; scoreIndex < studentSum; scoreIndex++)
    {
      for(int j=0; j<studentSum; j++) 
      {
        Console.WriteLine("Enter score");
        var score = Console.ReadLine();
        Console.WriteLine(string.Empty);
        int parsedScore = 0;
        success = int.TryParse(score, out parsedScore);

        studentScores[scoreIndex,j] = parsedScore;
      }
    }