C# 不可达代码与验证

C# 不可达代码与验证,c#,dictionary,C#,Dictionary,我(非常)不熟悉C#,试图从.txt文件中获取字符串列表,并创建我想要显示的最常见的前20个字符串。这是一个项目,我不会说谎,但我不明白为什么我不能达到代码的某一部分,并将感谢任何帮助 到目前为止,我的代码是: // I have used framework 4 public class Program { private static int Compare(KeyValuePair<string, int> kv1, KeyValuePair<string,

我(非常)不熟悉C#,试图从.txt文件中获取字符串列表,并创建我想要显示的最常见的前20个字符串。这是一个项目,我不会说谎,但我不明白为什么我不能达到代码的某一部分,并将感谢任何帮助

到目前为止,我的代码是:

// I have used framework 4  
public class Program
{

    private static int Compare(KeyValuePair<string, int> kv1, KeyValuePair<string, int> kv2)
    {
        return kv2.Value == kv1.Value ? kv1.Key.CompareTo(kv2.Key) : kv2.Value - kv1.Value;
    }

    public static void Main()
    {
        {
            try
            {
                Dictionary<string, int> histogram = new Dictionary<string, int>();      // creates a dictionary from the text file
                using (StreamReader reader = new StreamReader("tweets.txt"))        //reads the text file specified
                {
                    string difline;
                    while ((difline = reader.ReadLine()) != null)       //continues until no lines left
                    {
                        if (histogram.ContainsKey(difline))     //counts specific strings
                            ++histogram[difline];
                        else
                            histogram.Add(difline, 1);
                    }
                }


                using (StreamReader sr = new StreamReader("tweets.txt"))        // Create an instance of StreamReader to read from a file.
                // also closes the StreamReader.
                {
                    string line;
                    long linecount = linesinfile("tweets.txt");
                    Console.WriteLine(linecount);
                    TextWriter tw = new StreamWriter("tweets.html"); //create a writer
                    tw.WriteLine("<html> <body>");
                    while ((line = sr.ReadLine()) != null) // Read and display lines from the file until the end of the file is reached.
                    {
                        Console.WriteLine(line);
                        tw.WriteLine("{0} <br />", line); //write the lines of text to the html file

                    }
                    tw.WriteLine("</html> </body>");
                    tw.Close(); //close the writer
                }

            }

            catch (Exception e)
            {

                Console.WriteLine("The file could not be read:"); // Let the user know what went wrong.
                Console.WriteLine(e.Message);
            }
            Console.Write("\nPress any key to continue . . . ");
            Console.ReadKey(true);
        }
    }


    static long linesinfile(string l)
    {
        long count = 0;
        using (StreamReader r = new StreamReader(l))
        {
            string line;
            while ((line = r.ReadLine()) != null)       //counts lines until last line
            {
                if (line.StartsWith("#") & line.Length > 1)       //only count lines which start with #
                {
                    count++;    //increases the count
                }
            }
            return count;       //displays the line count
        }

        //this code is unreachable

        Dictionary<string, int> histogram = new Dictionary<string, int>();
        using (StreamReader reader = new StreamReader("tweets.txt"))
        {
            string line;

            while ((line = reader.ReadLine()) != null)
            {
                if (histogram.ContainsKey(line))
                    ++histogram[line];
                else
                    histogram.Add(line, 1);
            }
            {
                Console.Write("{0} ", histogram);
            }
        }

        List<KeyValuePair<string, int>> sortedHistogram = new List<KeyValuePair<string, int>>(histogram);
        sortedHistogram.Sort(Compare);
        foreach (KeyValuePair<string, int> kv in sortedHistogram)
            Console.WriteLine("{0}\t{1}", kv.Value, kv.Key);
    }
}
//我使用了框架4
公共课程
{
专用静态int比较(KeyValuePair kv1、KeyValuePair kv2)
{
返回kv2.Value==kv1.Value?kv1.Key.CompareTo(kv2.Key):kv2.Value-kv1.Value;
}
公共静态void Main()
{
{
尝试
{
字典直方图=新建字典();//从文本文件创建字典
使用(StreamReader=newstreamreader(“tweets.txt”)//读取指定的文本文件
{
弦二燧石;
而((difline=reader.ReadLine())!=null)//将继续,直到没有剩余的行为止
{
if(histogram.ContainsKey(difline))//统计特定字符串
++直方图[difline];
其他的
直方图。添加(difline,1);
}
}
使用(StreamReader sr=newstreamreader(“tweets.txt”)//创建StreamReader的实例以从文件中读取。
//还将关闭StreamReader。
{
弦线;
long linecount=linesinfile(“tweets.txt”);
控制台写入线(行计数);
TextWriter tw=newstreamwriter(“tweets.html”);//创建一个writer
tw.WriteLine(“”);
while((line=sr.ReadLine())!=null)//读取并显示文件中的行,直到到达文件末尾。
{
控制台写入线(行);
WriteLine(“{0}
”,第行);//将文本行写入html文件 } tw.WriteLine(“”); tw.Close();//关闭编写器 } } 捕获(例外e) { Console.WriteLine(“文件无法读取:”);//让用户知道出了什么问题。 控制台写入线(e.Message); } 控制台。写入(“\n按任意键继续…”); Console.ReadKey(true); } } 静态长线文件(字符串l) { 长计数=0; 使用(StreamReader r=新StreamReader(l)) { 弦线; while((line=r.ReadLine())!=null)//计算直到最后一行的行数 { if(line.StartsWith(“#”)&line.Length>1)//只计算以开头的行# { count++;//增加计数 } } return count;//显示行数 } //此代码无法访问 字典直方图=新字典(); 使用(StreamReader=newstreamreader(“tweets.txt”)) { 弦线; 而((line=reader.ReadLine())!=null) { if(柱状图、容器(线)) ++直方图[线]; 其他的 直方图。添加(第1行); } { 写入(“{0}”,直方图); } } 列表分类直方图=新列表(直方图); 排序(比较); foreach(分类直方图中的键值对kv) Console.WriteLine(“{0}\t{1}”,千伏值,千伏键); } }
A
return
语句停止当前方法的执行,并将控制权返回给调用方。在您的情况下,您似乎有过早的
返回计数linesinfile
方法中的code>语句。尝试移动
返回计数
行sinfile
方法的末尾,就在关闭
}
之前,它应该可以工作。

A
return
语句停止当前方法的执行,并将控制权返回给调用方。在您的情况下,您似乎有过早的
返回计数linesinfile
方法中的code>语句。尝试移动
返回计数
行sinfile
方法的末尾,就在关闭
}
之前,它应该可以工作

static long linesinfile(string l)
        {
            long count = 0;
            using (StreamReader r = new StreamReader(l))
            {
                string line;
                while ((line = r.ReadLine()) != null)       //counts lines until last line
                {
                    if (line.StartsWith("#") & line.Length > 1)       //only count lines which start with #
                    {
                        count++;    //increases the count
                    }
                }
                return count;       //displays the line count
            }
在函数中执行无条件的
返回计数
,因此此函数中的其余代码通常无法访问

在函数中执行无条件的
返回计数
,因此此函数中的其余代码通常无法访问。

此代码

return count;       //displays the line count
是无条件执行的,这意味着它结束了方法的执行。。。这将导致无法访问该方法中的其余代码。

此代码

return count;       //displays the line count

是无条件执行的,这意味着它结束了方法的执行。。。这会导致无法访问该方法中的其余代码。

您的代码包含大量冗余。我试着将其精简,并将前20个结果显示到控制台(消除HTML文件输出)

//我使用了框架4
使用制度;
使用System.IO;
使用System.Linq;
使用System.Collections.Generic;
公共课程
{
专用静态int比较(KeyValuePair kv1、KeyValuePair kv2)
{
返回kv2.Value.CompareTo(kv1.Value);//降序
}
公共静态void Main()
{
尝试
{
int linecount=0;
字典直方图=新建字典();//从文本文件创建字典
使用(StreamReader=newstreamreader(“tweets.txt”)//读取文本