【问题标题】:take one item from list and import it to another list从列表中取出一项并将其导入另一个列表
【发布时间】:2021-11-30 18:28:53
【问题描述】:

所以,长话短说,我想把缺席的人只导入缺席名单,每次我尝试这样做时,它都会从学生名单中导入所有人,所以没有多大帮助;

这样在最后我可以清除并显示每个学生的出勤颜色,如果他们缺席,则为红色,如果他们在场,则为绿色

提前 :)

using System.Collections.Generic;

namespace HelloWorld
{
    class Hello
    {
        static void Main(string[] args)
        {
            List<string> students = new List<string>(5);
            students.Add("Obama Ojane"); 
            students.Add("Yarik Ze");
            students.Add("Allen O'neill");
            students.Add("Naveen Sharma");
            students.Add("Monica Rathbun");
            students.Add("David McCarter");
            students.Add("Zayne Pan");
            students.Add("Rah");

            int v = 0;
            int abs = 0;
            int pres = 0;

            List<string> absent = new List<string>(5);
            List<string> present = new List<string>(5);

            foreach (string a in students)
            {
                Console.Write(students[v] + ": ");
                string attendance = Console.ReadLine();
                if (attendance == "absent" || attendance == "abs" || attendance == "a")
                {
                    abs++; 
                    absent.Append(attendance);
                    String studentNameA = students[v];
                    absent.Add(studentNameA);
                }
                if (attendance == "present" || attendance == "pres" || attendance == "p")
                {
                    pres++;
                    present.Append(attendance);
                    String studentNameP = students[v];
                    absent.Add(studentNameP);

                }
                v++;
            }
            Console.WriteLine();
            Console.Clear();
            Console.WriteLine("total present: " + pres);
            Console.WriteLine("total absent: " + abs);
            Console.WriteLine("total students: " + (abs+pres));



            Console.ForegroundColor = ConsoleColor.Red;
            for (int i = 0; i < absent.Count; i++)
            {
                Console.WriteLine(absent[i]);
            }

            Console.ForegroundColor = ConsoleColor.Green;
            for (int i = 0; i < present.Count; i++)
            {
                Console.WriteLine(present[i]);
            }

        }
    }
}

【问题讨论】:

  • 你很清楚在“现在”的情况下有absent.Add(studentNameP);,这不可能是正确的
  • 这是开始使用调试器的好机会。使用调试器,您可以暂停代码的执行并逐行执行该执行,观察确切的运行时行为以及变量如何随每一行代码变化。当您这样做时,哪个特定操作首先会产生意想不到的结果?该操作中使用的值是什么?结果如何?预期的结果是什么?为什么?

标签: c# list


【解决方案1】:

复制 + 粘贴邪恶absent.Add(studentNameP); as UnholySheep 放入 cmets 绝对是个问题。

让我们实现例程:

我们从数据开始:

// Keep it simple with a help of syntax sugar
var students = new List<string>() {
  "Obama Ojane",
  "Yarik Ze",
  "Allen O'neill",
  "Naveen Sharma",
  "Monica Rathbun",
  "David McCarter",
  "Zayne Pan",
  "Rah",
};

// avoid magic constants: what did 5 stand for? 
var absent = new List<string>(students.Count);
var present = new List<string>(students.Count);

然后我们添加UI,我们将学生分配到absentpresent

foreach(var student in students) {
  // List (either present or absent) in which we add 
  List<string> list = null;

  // Keep asking user until absent or present is selected
  do {
    Console.Write($"{student}: ");

    string attendance = Console.ReadLine().Trim();

    if (attendance == "absent" || attendance == "abs" || attendance == "a")
      list = absent;  // we add to absent
    else if (attendance == "present" || attendance == "pres" || attendance == "p")
      list = present; // we add to present
  }
  while (list == null); 

  list.Add(student);
}

最后,我们输出

Console.Clear();
Console.WriteLine($"total present:  {present.Count}");
Console.WriteLine($"total absent:   {absent.Count}");
Console.WriteLine($"total students: {present.Count + absent.Count}");

Console.ForegroundColor = ConsoleColor.Red;

foreach (var student in absent)
  Console.WriteLine(student);

Console.ForegroundColor = ConsoleColor.Green;

foreach (var student in present)
  Console.WriteLine(student); 

【讨论】:

    【解决方案2】:

    您的一个问题是您试图让一种方法Main() 做得太多。如果你把事情分解成每个做一件事的离散方法,事情就会变得简单得多。也更容易理解:

    using System.Collections.Generic;
    
    namespace HelloWorld
    {
      class Hello
      {
          static void Main(string[] args)
          {
            List<string> students = new List<string>(new string[]{
              "Obama Ojane",
              "Yarik Ze",
              "Allen O'neill",
              "Naveen Sharma",
              "Monica Rathbun",
              "David McCarter",
              "Zayne Pan",
              "Rah",
            });
            List<string> absent = new List<string>();
            List<string> present = new List<string>();
    
            foreach (string student in students)
            {
              bool isPresent = getStatus(student);
              if (isPresent)
              {
                present.Add(student);
              }
              else
              {
                absent.Add(student);
              }
            }
    
            WriteSummary(present.Count, absent.Count, students.Count);
            WriteReport(absent, "Absent", ConsoleColor.Red);
            WriteReport(present, "Present", ConsoleColor.Green);
    
          }
    
          static void WriteSummary(int present, int absent, int total)
          {
            Console.Clear();
            Console.WriteLine("total present: {0}", present);
            Console.WriteLine("total absent: {0}", absent);
            Console.WriteLine("total students: {0}", students);
          }
    
          static void WriteReport(List<string> students, string title, ConsoleColor color)
          {
            Console.WriteLine();
            Console.WriteLine("{0}:", title);
            Console.ForegroundColor = color;
            foreach (var student in students)
            {
                Console.WriteLine(student);
            }
          }
    
          static bool getStatus(string student)
          {
    
            // loop forever
            while (true)
            {
    
              // prompt with the student's name
              Console.Write("{0}: ", student);
    
              // read the response
              string status = Console.ReadLine();
    
              // test the response
              switch (status.ToLower())
              {
              case "a":
                // student is absent
                return false;
              case "p":
                // student is present
                return true;
              default:
                // otherwise, the response is is incorrect/unknown.
                // report the error and prompt again
                Console.WriteLine("Please enter 'a' for absent or 'p' for present");
              }
    
            }
    
          }
    
      }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2014-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多