如何在 C# 中将一定数量的字符串从一个列表添加到另一个列表

How to add certain number of strings from one list to another in c#

我想做的是制作两个列表,每个列表包含一定数量的学生。我想问用户他们想从 group1 发送多少学生到 group2。用户输入后,程序应从第一个列表 (group1) 中删除该数量的学生并将其添加到第二个列表 (group2)。

这是我到目前为止所写的。我的问题是我不知道如何将一定数量的字符串从一个列表添加到另一个列表。 到目前为止,我只是将一组中的所有元素添加到另一组中,但没有添加一定数量的元素。

 class Program
    {
        static void Main(string[] args)
        {
            var groupOne = new List<string>() {"student_G1_01", "student_G1_02", "student_G1_03", "student_G1_04", "student_G1_05" };
            var groupTwo = new List<string>() { "student_G2_01", "student_G2_02", "student_G2_03", "student_G2_04", "student_G2_05", "student_G2_06" };

          
            Console.WriteLine("Student of group 1:");
            foreach (var strudent in groupOne)
            {

                Console.WriteLine(strudent);
            }

            Console.WriteLine("\n total number of students:" + groupOne.Count);



            Console.WriteLine("\n -------------- \n");



            Console.WriteLine("Students of group 2:");
            foreach(var student in groupTwo)
            {
                Console.WriteLine(student);
            }

            Console.WriteLine("\n total number of students:" + groupTwo.Count);



            Console.WriteLine("\n -------------- \n");


            //adding all the students from group2 to group1 

            groupOne.AddRange(groupTwo);


            foreach (var strudent in groupOne)
            {

                Console.WriteLine(strudent);
            }

            Console.WriteLine("\n total number of students:" + groupOne.Count);

        }

试试这个,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace HelloWorld
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var groupOne = new List<string>() {"student_G1_01", "student_G1_02", "student_G1_03", "student_G1_04", "student_G1_05" };
            var groupTwo = new List<string>() { "student_G2_01", "student_G2_02", "student_G2_03", "student_G2_04", "student_G2_05", "student_G2_06" };
        
            var userInput = 3; // user input
            groupTwo.AddRange(groupOne.Take(userInput));
          
            foreach(var group in groupTwo){
              Console.WriteLine(group);  
            }             
        }
    }
}

试试这个,已经过测试

var groupOne = new List<string>() { "student_G1_01", "student_G1_02", "student_G1_03", "student_G1_04", "student_G1_05" };
var groupTwo = new List<string>() { "student_G2_01", "student_G2_02", "student_G2_03", "student_G2_04", "student_G2_05", "student_G2_06" };

    var userInput = 3; // user input
    var take= groupOne.Take(userInput);
    groupTwo.AddRange(take);
   groupOne.RemoveRange(0,userInput) ;
    
     Console.WriteLine("------Group 1-------");
    foreach (var group in groupOne)
    {
        Console.WriteLine(group);
    }
   Console.WriteLine("------Group 2-------");
    foreach (var group in groupTwo)
    {
        Console.WriteLine(group);
    }

测试结果

------Group 1-------
student_G1_04
student_G1_05
------Group 2-------
student_G2_01
student_G2_02
student_G2_03
student_G2_04
student_G2_05
student_G2_06
student_G1_01
student_G1_02
student_G1_03