无法访问的代码和验证
本文关键字:验证 代码 访问 | 更新日期: 2023-09-27 18:20:16
我(非常)不熟悉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);
}
}
return
语句停止执行当前方法并将控制权返回给调用方。在您的案例中,您的linesinfile
方法中似乎有一个过早的return count;
语句。尝试将return count;
移动到linesinfile
方法的端,就在关闭}
之前,它应该可以工作。
此代码
return count; //displays the line count
无条件执行,这反过来意味着它结束了方法的执行。。。这导致该方法中的其余代码无法访问。
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
,因此该函数中的其余代码通常是不可访问的。
您的代码包含大量冗余。我试着把它修剪掉,并把前20个结果显示到控制台(去掉HTML文件输出)。
// I have used framework 4
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
public class Program
{
private static int Compare(KeyValuePair<string, int> kv1, KeyValuePair<string, int> kv2)
{
return kv2.Value.CompareTo(kv1.Value); // descending order
}
public static void Main()
{
try
{
int linecount = 0;
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
{
linecount++; //increases the count
if (histogram.ContainsKey(difline)) //counts specific strings
++histogram[difline];
else
histogram.Add(difline, 1);
}
}
Console.WriteLine("Line count: " + linecount);
Console.WriteLine("Top 20:");
List<KeyValuePair<string, int>> sortedHistogram = new List<KeyValuePair<string, int>>(histogram);
sortedHistogram.Sort(Compare);
foreach (KeyValuePair<string, int> kv in sortedHistogram.Take(20))
Console.WriteLine("{0}'t{1}", kv.Value, kv.Key);
}
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);
}
}