【问题标题】:Printing 2 values in a single foreach loop (or other solution)在单个 foreach 循环(或其他解决方案)中打印 2 个值
【发布时间】:2019-03-15 17:14:40
【问题描述】:

我正在编写一个基本程序来打印学生的姓名和年级(都在一个数组中)。当我尝试再次打印数组时出现错误(索引超出数组范围),我知道我必须打印什么我只是不知道我应该如何保存不同的数组输入并在循环中显示它们.

static void Main(string[] args)
    {
        double average = 0;
        //double Hoogste = 0;
        double sum = 0;
        int i;
        Console.Write("lesson: ");
        string lesson = Console.ReadLine();
        Console.Write("number of students: ");
        int numStudents = int.Parse(Console.ReadLine());
        Console.WriteLine("\n");
        string[] names = new string[numStudents];
        int[] grade = new int[numStudents];

        for (i = 0; i < numStudents; i++)
        {
            Console.Write("name? ");
            names[i] = Console.ReadLine();
            Console.Write("grade? ");
            grade[i] = int.Parse(Console.ReadLine());

            sum += grade[0];
            average = sum / numStudents;

        }

        foreach (string item in names) ;
        {
            Console.WriteLine($"The grade of {names[i]} is {grade[]i}");
        }

【问题讨论】:

  • i get an error 总是告诉我们错误是什么,这样我们就不必猜测了

标签: c# arrays list loops


【解决方案1】:

您的代码无法按原样编译。我会给你怀疑的好处,并假设这是一个复制粘贴错误。我已经纠正了您在以下代码中遇到的错误编译时间

您的主要问题是您在循环范围之外声明了loop variablei,这使其可用于您打印的下一个循环。您的打印循环有一些问题。您使用foreach 循环遍历names 数组,但使用索引i 访问names 数组。使用内联 cmets 查看下面的代码

static void Main(string[] args) {
    double average = 0;
    //double Hoogste = 0;
    double sum = 0;
    //int i; // do not declare it here, this was causing you issues

    Console.Write("lesson: ");
    string lesson = Console.ReadLine();

    Console.Write("number of students: ");
    int numStudents = int.Parse(Console.ReadLine());

    Console.WriteLine("\n");

    string[] names = new string[numStudents];
    int[] grade = new int[numStudents];

    for (int i = 0; i < numStudents; i++) { // declare the loop variable here
        Console.Write("name? ");
        names[i] = Console.ReadLine();

        Console.Write("grade? ");
        grade[i] = int.Parse(Console.ReadLine());

        sum += grade[i]; // i presume you don't want to do grade[0] but rather grade[i]
    }

    average = sum / numStudents; // I presume you don't want this line inside the for-loop, if you expect the average to be properly calculated

    //foreach (string item in names) // there was a semi-colon here by mistake, which should not be there
    for (int i = 0; i < numStudents; ++i) // you want to loop over the index
    {
        Console.WriteLine($"The grade of {names[i]} is {grade[i]}"); // i was outside the square brackets like grade[]i 
    }
}

【讨论】:

    【解决方案2】:

    从两个小改动开始:

    1. 第一个 for 循环需要为 i for (int i = 0; 声明一个类型,而在 foreach 循环中,i 超出范围,因此您将无法使用它。您可能可以移除 foreach 循环并将 Console.WriteLine 放在第一个 foreach 循环的底部。

    2. 另外,用{grade[]i}检查语法

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-08
      • 2013-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多