【问题标题】:Getting rank for baby name popularity ranking program获得婴儿名字人气排名程序的排名
【发布时间】:2017-12-13 02:23:14
【问题描述】:

我正在尝试完成此程序,但一直遇到问题,我无法弄清楚如何获得结果排名的计数我的输出一直为 1,我正在尝试完成此程序我会尽快。输出应该是这样的:

输入数据文件的目录路径:C:\Intel
进入年份:2010
输入性别(M/F):M
输入姓名:哈维尔

哈维尔在 2010 年排名第 190 位

namespace QuestionTwo
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> names = new List<string>();

            Console.Write("Enter the directory path of the data files: ");
            string filename = Console.ReadLine();
            filename.Replace(@"\", @"\\");

            Console.Write("Enter the year: ");
            string year = Console.ReadLine();

            Console.Write("Enter the gender (M/F): ");
            string gender = Console.ReadLine();

            Console.Write("Enter the name: ");
             string name = Console.ReadLine();

            filename += "\\yob" + year + ".txt";
            string line = "";
            try
            {
                using (StreamReader fin = new StreamReader(filename))
                {
                    while((line = fin.ReadLine()) != null)
                    {
                        string[] parsed = line.Split(',');

                        if (parsed[1] == gender)
                        {
                                names.Add(parsed[0]);
                        }
                    }
                }

                for (int i = 0; i < names.Count; i++)
                {
                    if (names[i] == (name)) 
                    {
                        Console.WriteLine(name + " is ranked #" + i + " in year " + year);
                    }
                    else //if (names[i] != (name)) // it is a logic error somewhere here 
                    {
                        Console.WriteLine("The name " + name
                            + " is not ranked in year " + year);
                    }
                }
            }
            catch(Exception e  )
            {
                Console.WriteLine("The file at {0} could not be read."+ e,  filename);
            }

            for(int c = 0; c < names.Count; c++)
            {
                Console.WriteLine((c + 1) + ". " + names[c]);
            }
            Console.ReadKey();
        }
    }
}

【问题讨论】:

  • 尝试调试代码以确保值符合您的预期。
  • 我做到了,它一直保持在一个
  • 你从文件中得到了真正的价值吗?
  • 在这部分for (int i = 0; i &lt; names.Count; i++) 中,名称是否包含您需要的确切内容?
  • 尝试从您的 .text 文件中添加真实的示例数据。

标签: c# console readline ranking


【解决方案1】:

您的程序输出太多,因为您正在打印 "The name" + name 对于每个不匹配的名称,+" 不在年" + 年"中排名。

您需要添加一个标志来记录您在查看列表时是否找到了该名称,如下所示:注意我还添加了一个break 关键字 - 找到名称后,无需继续看着。

您还需要从 i 中减去 1,因为您的列表从 0 开始,我希望您不想要从零开始的排名?

class Program
{
    static void Main(string[] args)
    {
        List<string> names = new List<string>();

        string filename = @"c:\temp\baby.txt";
        string year = "1997";
        string gender = "F";
        string name = "Margaret";

        string line = "";
        try
        {
            using (StreamReader fin = new StreamReader(filename))
            {
                while((line = fin.ReadLine()) != null)
                {
                    string[] parsed = line.Split(',');

                    if (parsed[1] == gender)
                    {
                            names.Add(parsed[0]);
                    }
                }
            }

            bool foundName = false;

            for (int i = 0; i < names.Count; i++)
            {
                if (names[i] == (name)) 
                {
                    Console.WriteLine(name + " is ranked #" + i-1 + " in year " + year);
                    foundName = true;
                    break;
                }
            }

            if (!foundName)
            {
                 Console.WriteLine("The name " + name
                        + " is not ranked in year " + year);
            }

        }
        catch(Exception e  )
        {
            Console.WriteLine("The file at {0} could not be read."+ e,  filename);
        }

        for(int c = 0; c < names.Count; c++)
        {
            Console.WriteLine((c + 1) + ". " + names[c]);
        }
        Console.ReadKey();
    }
}

输出:

玛格丽特在 1997 年排名第四
Doris 这个名字在 1997 年没有排名

注意事项:

  • 您的搜索区分大小写,因此 "margaret" 不匹配
  • 您的性别检查也区分大小写,因此 "f" 找不到匹配项

【讨论】:

  • 性别后面的数字是为了告诉有多少人使用了这个名字,但我正在寻找我认为可以获得排名的索引号,因为当用户输入 m 或 f 时,它会自动将它们分成单独的数组
  • 我改变了答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-22
  • 1970-01-01
  • 2014-05-24
  • 2015-01-29
  • 1970-01-01
  • 1970-01-01
  • 2018-12-19
相关资源
最近更新 更多