C# c程序错误,括号错位?
我一直得到一个错误行50第13列,名称空间不能直接包含成员,如字段或方法 第47行第17列也是预期的捕获或最终捕获 代码是控制台模式下的C程序,使用C System.IO库将数据文件作为顺序文件加载到使用System.Collection库的ArrayList数据结构中,并将文件中的每一行存储为单独的记录。然后将文件加载到ArrayList中,根据LastName字段按升序对数据进行排序,并显示以下字段:接下来,根据ZIP字段按降序对数据进行排序,并显示以下字段:最后显示处于NY状态的每个人的所有记录及其所有字段C# c程序错误,括号错位?,c#,C#,我一直得到一个错误行50第13列,名称空间不能直接包含成员,如字段或方法 第47行第17列也是预期的捕获或最终捕获 代码是控制台模式下的C程序,使用C System.IO库将数据文件作为顺序文件加载到使用System.Collection库的ArrayList数据结构中,并将文件中的每一行存储为单独的记录。然后将文件加载到ArrayList中,根据LastName字段按升序对数据进行排序,并显示以下字段:接下来,根据ZIP字段按降序对数据进行排序,并显示以下字段:最后显示处于NY状态的每个人的所
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication24
{
class dataItem
{
// this class will hold the data from one string in the file
string FirstName;
string LastName;
// etc.
}
class Program
{
static void Main(string[] args)
{
// create a place to store our data
ArrayList data = new ArrayList();
// open and read a text file
StreamReader sr = new StreamReader("Unit4IP");
try
{
using (StreamReader sr = new StreamReader("Unit4IP"))
{
// need a string to hold the fields and a string variable for each line of text.
String line;
string[] fields;
while ((line = sr.ReadLine()) != null);
}
fields = line.Split(new Char [] {','});
Console.WriteLine(fields[0]);
Console.WriteLine(fields[1]);
// from here you can create a data to store your fields in it
// then insert the object into your data ArrayList.
// after that, you can do the sorting and output after this loop is done reading
}
}
}
catch (Exception e)
{
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
// Store text file contents in an appropriate data structure
// Sort data
// Output data
// put this at the end to keep the console window from disappearing
Console.WriteLine("Press enter to continue..");
Console.ReadLine();
}
}
这似乎是错误的:
while ((line = sr.ReadLine()) != null);
}
我从缩进和大括号之间的不匹配中猜测,您不需要那个分号,并且右大括号应该是右大括号。可能还有其他问题,但首先要做的是对照缩进检查大括号。这看起来是错误的:
while ((line = sr.ReadLine()) != null);
}
我从缩进和大括号之间的不匹配中猜测,您不需要那个分号,并且右大括号应该是右大括号。可能还有其他问题,但首先要做的是对照缩进检查大括号。缺少右括号}从外观上看,在结尾处。在Try=>use=>之后,虽然有4个右括号,但只有2个左括号…在窗口中选择所有代码ctrl-A,然后将其折叠ctrl-M-M,按住ctrl键的同时按M两次。这样,您将看到缺少的括号在哪里。缺少的结束括号}从外观上看。在Try=>use=>之后,虽然有4个结束括号,但只有2个开始括号…在窗口中选择所有代码ctrl-A,然后将其折叠ctrl-M-M,按住ctrl键的同时按两下M键。这样你就会看到缺少的括号在哪里了。