so, long story short i want to take the absent person and import only that one person to the absent list, every time i tried this it imported everyone from the students list so that isnt much help;
this is so at the very end i can clear and display every student with the respective colors for their attendance, if theyre absent they're red, and if they were present they are green
ty in advance :)
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]);
}
}
}
}
CodePudding user response:
Copy Paste is evil: absent.Add(studentNameP);
as UnholySheep put in comments is definitely a problem.
Let's implement the routine:
We start from data:
// 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);
Then we add UI where we assign students into absent
or present
:
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);
}
Finally, we output:
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);
CodePudding user response:
One of your problems is that your are trying to have one method Main()
do way too much. If you break things up into discrete methods that each do one thing, stuff becomes much simpler. Also easier to understand:
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");
}
}
}
}
}