简体   繁体   English

如何创建一个 if 语句,从 csv 文件中输出两个具有相同分钟数的学生姓名

[英]how do i create an if statement that outputs two names of students who have the same number of minutes from csv file

I am trying to output two names of students who have the same number of mins from a CSV file我正在尝试从 CSV 文件中输出两个具有相同分钟数的学生姓名

input file :输入文件 :

ID,Last Name,First Name,Phone Number,Minutes ID、姓氏、名字、电话号码、分钟

1,Doe,John,905-555-5555,1020 1,Doe,约翰,905-555-5555,1020

2,Barnett,Courtney,905-666-6666,112 2,巴内特,考特尼,905-666-6666,112

3,Morrison,Jim,905-777-7777,912 3,莫里森,吉姆,905-777-7777,912

4,Doe,Jane,905-222-2222,1020 4,多伊,简,905-222-2222,1020

5,Mitchell,Joni,416-333-3333,112 5,米切尔,乔尼,416-333-3333,112

(ignore spaces between each line) (忽略每行之间的空格)

here Courtney and Joni have the same number of minimum minutes...i need to output both their names这里 Courtney 和 Joni 的最小分钟数相同......我需要输出他们的名字

my code is:我的代码是:

// open input file
StreamReader sr = new StreamReader("input.txt");
{
// initialize variables
int x=0,total= 0; double average=0;
string? titles,lastFirst;
string [ ] info, titlesSplit; int [ ] mins= new int [5]; string[,] myName = new string[5,5];

{
titles =sr.ReadLine();
titlesSplit = titles.Split(',');

for (x=0;x < 5; x++)
{
    System.Console.Write(titlesSplit[x] + " ");
}
 System.Console.WriteLine();
 System.Console.WriteLine("-------------------------------------------------");

for(x = 0; x < 5 ; x++)
{  
    lastFirst = sr.ReadLine(); 
    info =  lastFirst.Split(',');
    
    for (int k =0; k<5; k ++)
    {
        myName[x,k]= info[k];
    }
}

int maxMin = Int32.MinValue;
int minMin = Int32.MaxValue;

string? maxName = " ";
string? maxLastName = " ";
string? minName=" ";
string? minLastName = " ";

for (int l = 0; l<5; l++)
{
    for (int k =0; k <5;k++)
    {
        System.Console.Write(myName[l,k] + " ");
    }
    System.Console.WriteLine();
    

    if (Convert.ToInt32(myName[l,4]) > maxMin)
    {
        maxMin = Convert.ToInt32(myName[l,4]);
        maxName = myName[l,1];
        maxLastName= myName[l,2];
        //if statement to output two names of students who have the same number of mins 
    }
  
    if(Convert.ToInt32(myName[l,4]) > minMin );
    {
        minMin= Convert.ToInt32(myName[l,4]);
        minName= myName[l,1];
        minLastName= myName[l,2];

        //if statement to output two names of students who have the same number of 
        mins 
     
    }  
      total = (total + Convert.ToInt32(myName[l, 4]));
} 

    average = (total / 5);
    System.Console.WriteLine("-------------------------------------------------");
    System.Console.WriteLine("Minimum: " + minName +" " + minLastName +" " + minMin);
    System.Console.WriteLine("Maximum: " + maxName +" " + maxLastName+ " "+ maxMin);
    System.Console.WriteLine("Average: " + average);

}
    
}

sr.Close(); 

I took the liberty to change up your code a bit.我冒昧地更改了您的代码。 For example, I think it would be a good idea to create a model class for your line entries:例如,我认为为您的行条目创建一个模型类是个好主意:

public class Person
{
    public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string PhoneNumber { get; set; }
    public int Minutes { get; set; }

}

The code to output the names of the entries, that have the smallest amount of minutes might then look something like this:输出具有最少分钟数的条目名称的代码可能如下所示:

// open input file
using StreamReader sr = new StreamReader(Path);

List<Person> Persons = new();

_ = sr.ReadLine(); // ignore first line

while (!sr.EndOfStream)
{
    var line = sr.ReadLine();
    var values = line!.Split(',');

    Persons.Add(new Person
    {
        ID = int.Parse(values[0]),
        LastName = values[1],
        FirstName = values[2],
        PhoneNumber = values[3],
        Minutes = int.Parse(values[4])
    });
}

foreach(var person in Persons.OrderBy(p => p.Minutes).GroupBy(m => m.Minutes).First())
{
    Console.WriteLine(person.FirstName);
}

It might be a good idea to add some more checks for the parsing and if you have big amounts of data, one might look into some performance improvements, but in general this should give some idea.为解析添加更多检查可能是个好主意,如果您有大量数据,可能会考虑一些性能改进,但总的来说这应该给出一些想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM