有没有办法将阵列连接在一起? (Visual Basic控制台模式) [英] Is there a way to join arrays together? (Visual basic console mode)

查看:160
本文介绍了有没有办法将阵列连接在一起? (Visual Basic控制台模式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在执行任务。我刚刚学习了数组,我正在使用Visual Basic控制台模式。在赋值中,我声明了5个一维数组,它们都有30个变量:names,test1,test2,test3,total。它们是学生的名字,他们的考试成绩和他们取得的总分。可以输入名称,test1,test2和test3。问题是我必须计算总分,然后用相应的学生名输出。如何相应地添加来自不同数组的值,使其与学生姓名对应并输出学生姓名?



示例:

John是其中一名学生,他的分数存储在3个不同的阵列中。现在我需要在不同的数组中添加3个变量来获得他的总分,但不仅仅是John,而是总共30个学生。您可以假设每个数组中的变量对应于names数组。因此,如果John是names数组的第一个变量,那么test1,test2,test3的第一个变量就是他的分数,需要加在一起。关键是我必须为30名学生做这件事



我尝试过:


我不知道我可以用数组做什么,所以我一直在搜索我可以用数组做的例子或事情。我想我正在做类似于并行数组的事情。

解决方案

如果我理解正确,你想要得到数组中所有元素的总和。使用LINQ的扩展方法总和 [ ^ ]:

  Dim  sum  As   Integer  = yourArray.Sum()


Quote:

必须计算总分,然后输出相应的学生姓名



如果我正确阅读并理解你的问题,这就是你要找的:

  class 学生
{
public string 名称{ get ; set ; }
public float 得分{获取; set ; }
}
私有 静态 void Main()
{
var List1 = new List< ;学生>
{
new 学生{Name = 学生1,分数= 78 .1f},
new 学生{姓名= 学生2,分数= 88f}
};

var List2 = new 列表< Student>
{
new 学生{Name = 学生3,分数= 52 .7f},
new 学生{姓名= 学生1,分数= 48f}
};

var results = List1.Concat(List2)
.GroupBy(x = > x.Name)
。选择(x = > new 学生
{
姓名= x.Key,
分数= x.Sum(y = > y.Score)
});

foreach var student in results)
{
Console.WriteLine(


学生:{student.Name}>得分:{student.Score:N2});
}
}



哪个输出:

学生:学生1>分数:126.10 
学生:学生2>分数:88.00
学生:学生3>分数:52.70



更新:我的不好,这是VB.NET版本:

 学生
公共 属性名称()作为 字符串
公开 属性得分()作为
结束

模块 Module1

Sub Main()

Dim List1 = 列表( 学生)()来自{
学生()使用 {
.Name = 学生1
.Score = 78 .1F
},
学生() {
.Name = 学生2
。分数= 88 .0F
}
}

Dim List2 = 列表( 学生)()来自{
学生()使用 {
.Name = 学生3
。分数= 52 .7F
},
学生()使用 {
.Name = < span class =code-string>学生1,
.Score = 48 .0F
}
}

Dim results = List1.Concat(List2)_
.GroupBy(功能(x)x.Name)_
选择功能( x)学生()使用 {
.Name = x.Key,
.Score = x.Sum( Function (y)y.Score)
})

对于 每个学生结果
Console.WriteLine(<跨度cl ass =code-string> 学生:{student.Name}>分数:{student.Score:N2}
下一步

结束 Sub

结束 模块


Hi, I am currently stuck on an assignment. I have just learnt array and I am using Visual Basic console mode. In the assignment, I have declared 5 one-dimension arrays and they all have 30 variables: names, test1, test2, test3, total. They are names of students and their scores for tests and the total score that they have achieved. Names, test1, test2 and test3 can be input. The problem is that I have to calculate the total score and then output it with the corresponding student name. How do I add values from different arrays accordingly so that it corresponds with the student name and output the student name?

Example:
John is one of the students and his score is stored in 3 different arrays. Now I need to add 3 variables in different arrays to get his total score, but not just for John, but for 30 students in total. You can assume that the variable in every array corresponds to the "names" array. So if John is the first variable of the "names" array, then the first variable of the "test1", "test2", "test3" are his score and need to be added together. The point is I have to do this for 30 students

What I have tried:

I am not sure what I can do with arrays, so I have been searching for examples or things that I could do with arrays. I think I am doing things similar to a parallel array.

解决方案

If I understand correctly, you want to have the sum of all elements in the array. Use LINQ's extension method Sum[^] :

Dim sum As Integer = yourArray.Sum()


Quote:

have to calculate the total score and then output it with the corresponding student name


If I read and understood your question correctly, This is what you're looking for:

class Student
{
    public string Name { get; set; }
    public float Score { get; set; }
}
private static void Main()
{
    var List1 = new List<Student>
    {
        new Student { Name="Student 1", Score = 78.1f },
        new Student { Name="Student 2", Score = 88f }
    };

    var List2 = new List<Student>
    {
        new Student { Name="Student 3", Score = 52.7f },
        new Student { Name="Student 1", Score = 48f }
    };

    var results = List1.Concat(List2)
                       .GroupBy(x => x.Name)
                       .Select(x => new Student 
                                    {
                                        Name = x.Key,
                                        Score = x.Sum(y => y.Score)
                                    });

    foreach (var student in results)
    {
        Console.WriteLine(


"Student: {student.Name} > Score: {student.Score:N2}"); } }


Which outputs:

Student: Student 1  > Score: 126.10
Student: Student 2  > Score: 88.00
Student: Student 3  > Score: 52.70


UPDATE: My bad, Here is the VB.NET version:

Class Student
    Public Property Name() As String
    Public Property Score() As Single
End Class

Module Module1

    Sub Main()

        Dim List1 = New List(Of Student)() From {
            New Student() With {
                .Name = "Student 1",
                .Score = 78.1F
            },
            New Student() With {
                .Name = "Student 2",
                .Score = 88.0F
            }
        }

        Dim List2 = New List(Of Student)() From {
            New Student() With {
                .Name = "Student 3",
                .Score = 52.7F
            },
            New Student() With {
                .Name = "Student 1",
                .Score = 48.0F
            }
        }

        Dim results = List1.Concat(List2) _
                           .GroupBy(Function(x) x.Name) _
                           .Select(Function(x) New Student() With {
            .Name = x.Key,
            .Score = x.Sum(Function(y) y.Score)
        })

        For Each student In results
            Console.WriteLine("Student: {student.Name}  > Score: {student.Score:N2}")
        Next

    End Sub

End Module


这篇关于有没有办法将阵列连接在一起? (Visual Basic控制台模式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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