c程序错误,括号错位
本文关键字:错位 程序 错误 | 更新日期: 2023-09-27 18:28:46
我在第50行第13列得到一个错误,"命名空间不能直接包含字段或方法等成员"还有第47行第17列"预期catch或finally"代码将是控制台模式下的C#程序,使用C#System.IO库将数据文件作为顺序文件加载到ArrayList数据结构中(使用System.Collection库)。将文件中的每一行存储为单独的记录。然后将文件加载到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);
}
根据缩进和大括号之间的不匹配,我猜你不想要分号,并且最后的大括号应该是开头的大括号。可能还有其他问题,但首先要做的是根据缩进检查大括号。