C#Arrays - 在数组中打印多个值

时间:2022-12-05 10:05:25

I am working on a problem involving arrays. I am recording the marks of students entered into the system, two marks per line, and then storing them into two seperate arrays (Test One and test two). If the user enters e, the system stops asking for marks and prints out the values of the marks for each student.

我正在研究涉及数组的问题。我正在记录进入系统的学生的标记,每行两个标记,然后将它们存储到两个单独的数组中(测试一和测试二)。如果用户输入e,系统将停止询问标记并打印出每个学生的标记值。

        using System;
    class Marks
    {
        static void Main()
        {

            string[] input;
            string[] testOne = new string[40];
            string[] testTwo = new string[40];
            int count = 0;



            Console.WriteLine("You are to enter two test marks per student.");
            Console.WriteLine("Enter the letter e to end");

            for (int i = 0;  i <= 40; i++)
            {
                Console.Write("Enter marks for student " + (i+1) + ": ");
                input = Console.ReadLine().Split(',');

                if (input[0] == "e") break;
                else if (input[0] != "e")
                {
                    testOne[i] = input[0];
                    testTwo[i] = input[1];
                }


                count++;
            }

            Console.WriteLine("\nStudent  Test 1   Test 2   Total");
            Console.WriteLine("=======  ======   ======   =====");
            for (int i = 1; i <= count; i++)
            {
                Console.WriteLine(i + " " + testOne[i] + " " + testTwo[i]);
            }




        }
}

For the last block of code, where I am outputting the values of the array, I cannot seem to get it right. I'm trying to output the value stored in the array for each student.

对于最后一段代码,我输出数组的值,我似乎无法正确。我正在尝试为每个学生输出存储在数组中的值。

Any help would be appreciated!

任何帮助,将不胜感激!

UPDATE:

Thanks for the help!

谢谢您的帮助!

The sample output is as follows,

样本输出如下,

    You are to enter two test marks per student.
Enter the letter e to end.
Enter marks for student 1: 10, 14
Enter marks for student 2: 13, 9
Enter marks for student 3: 11, 7
Enter marks for student 4: 0, 18
Enter marks for student 5: e

Student  Test 1   Test 2   Total
=======  ======   ======   =====
    1     10.0     14.0     26.0
    2     13.0      9.0     24.0
    3     11.0      7.0     20.0
    4      0.0     18.0     27.0

As you can see, I can enter multiple marks for each student, up till 40 students or until the user enters e. Don't worry about total values as I can sort that out myself, but for now I simply want to get the values outputted.

如您所见,我可以为每个学生输入多个分数,直到40个学生或者直到用户输入e。不要担心总值,因为我可以自己解决这个问题,但是现在我只想得到输出的值。

2 个解决方案

#1


2  

I don't know exactly what the problem is. What I see is the following:

我不知道究竟是什么问题。我看到的是以下内容:

You create an array with 40 entries. Your first loop loops 41 times, so at the last position, you'll get an execption. The second loop starts by 1 and so, you are ignoring the first student. You should have the following code:

您创建一个包含40个条目的数组。你的第一个循环循环41次,所以在最后一个位置,你会得到一个execption。第二个循环从1开始,因此,您忽略了第一个学生。您应该具有以下代码:

For the first loop:

对于第一个循环:

for (int i = 0;  i < testOne.Length; i++)
{
}

And for your second loop:

对于你的第二个循环:

for (int i = 0; i < count; i++)
{
   Console.WriteLine((i+1) + " " + testOne[i] + " " + testTwo[i]);
}

EDIT:

To format it somehow like in your example, you can work with number formats and with \t (tab). Please be aware that you should check if the input is an int before parsing it (I don't do this in the example here, for an easier explanation). If you would like to right align it, you need to check if the value is < 10, and the add spaces etc. But remember, it's a console app, so the output don't need to be perfect in each case :)

要以某种方式格式化它,例如,您可以使用数字格式和\ t(制表符)。请注意,在解析之前应检查输入是否为int(我在此处的示例中不这样做,以便更容易解释)。如果你想对齐它,你需要检查值是否<10,以及添加空格等。但请记住,它是一个控制台应用程序,因此输出不需要在每种情况下都是完美的:)

Console.WriteLine("\nStudent\tTest 1\tTest 2\tTotal");
Console.WriteLine("=======\t======\t======\t=====");
for (int i = 0; i < count; i++)
{
   int one = int.Parse(testOne[i]);
   int two = int.Parse(testTwo[i]);

   Console.WriteLine((i + 1) + "\t" + one.ToString("0.0") + "\t" + two.ToString("0.0") + "\t" + (one + two).ToString("0.0"));
}

Or try what MAV mentioned in the comment :)

或者尝试MAV在评论中提到的:)

#2


0  

Try:

for (int i = 0; i <= count; i++)
{
    Console.WriteLine(i+1 + " " + testOne[i] + " " + testTwo[i]);
}

Also fix first loop so you don't overrun the array. Change:

还要修复第一个循环,这样就不会超出数组。更改:

for (int i = 0;  i <= 40; i++)

To:

for (int i = 0;  i < 40; i++)

#1


2  

I don't know exactly what the problem is. What I see is the following:

我不知道究竟是什么问题。我看到的是以下内容:

You create an array with 40 entries. Your first loop loops 41 times, so at the last position, you'll get an execption. The second loop starts by 1 and so, you are ignoring the first student. You should have the following code:

您创建一个包含40个条目的数组。你的第一个循环循环41次,所以在最后一个位置,你会得到一个execption。第二个循环从1开始,因此,您忽略了第一个学生。您应该具有以下代码:

For the first loop:

对于第一个循环:

for (int i = 0;  i < testOne.Length; i++)
{
}

And for your second loop:

对于你的第二个循环:

for (int i = 0; i < count; i++)
{
   Console.WriteLine((i+1) + " " + testOne[i] + " " + testTwo[i]);
}

EDIT:

To format it somehow like in your example, you can work with number formats and with \t (tab). Please be aware that you should check if the input is an int before parsing it (I don't do this in the example here, for an easier explanation). If you would like to right align it, you need to check if the value is < 10, and the add spaces etc. But remember, it's a console app, so the output don't need to be perfect in each case :)

要以某种方式格式化它,例如,您可以使用数字格式和\ t(制表符)。请注意,在解析之前应检查输入是否为int(我在此处的示例中不这样做,以便更容易解释)。如果你想对齐它,你需要检查值是否<10,以及添加空格等。但请记住,它是一个控制台应用程序,因此输出不需要在每种情况下都是完美的:)

Console.WriteLine("\nStudent\tTest 1\tTest 2\tTotal");
Console.WriteLine("=======\t======\t======\t=====");
for (int i = 0; i < count; i++)
{
   int one = int.Parse(testOne[i]);
   int two = int.Parse(testTwo[i]);

   Console.WriteLine((i + 1) + "\t" + one.ToString("0.0") + "\t" + two.ToString("0.0") + "\t" + (one + two).ToString("0.0"));
}

Or try what MAV mentioned in the comment :)

或者尝试MAV在评论中提到的:)

#2


0  

Try:

for (int i = 0; i <= count; i++)
{
    Console.WriteLine(i+1 + " " + testOne[i] + " " + testTwo[i]);
}

Also fix first loop so you don't overrun the array. Change:

还要修复第一个循环,这样就不会超出数组。更改:

for (int i = 0;  i <= 40; i++)

To:

for (int i = 0;  i < 40; i++)