我目前正在尝试使用 Stream Writer 和 Stream Reader 将数据输入文本文件以输出信息,在本例中为 - 名字、姓氏和年龄。然后使用我希望将其输出到下表中的信息:
First Name Surname Age
Etc Etc Etc
但是我在解决它时遇到了一些问题!
请在下面查看我的代码,谁能解释为什么我收到以下错误消息?
错误 1 使用未分配的局部变量 'firstname'
错误 2 使用未分配的局部变量“姓氏”
错误 3 使用未分配的局部变量 'age'
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{// Start namespace
class Program
{// Start Class
static void Main(string[] args)
{// Start Main
// Declaration Strings
String firstname; // Declares Name as a String
String lastname; // Declares Surname as a String
String age; // Declares Age as a String
int Counter; // Declares Counter as an Integer
FileInfo fleNames = new FileInfo("Names.txt"); // Creates fleNames as an Object, using Names.txt as the file name
StreamWriter swrNames = fleNames.CreateText(); // Informs fleName to Create Text
for (Counter = 0; Counter < 1; Counter++)
{
Console.Write("Enter Your First Name:"); // Writes users first name using Stream Writer Command
firstname = Console.ReadLine();
swrNames.WriteLine(firstname); // This dictates where the first name will be stored in the text file
swrNames.Flush();
Console.Write("Enter Your Surname"); // Writes users first name using Stream Writer Command
lastname = Console.ReadLine();
swrNames.WriteLine(lastname); // This dictates where the last name will be stored in the text file
swrNames.Flush();
Console.WriteLine("Enter Your Age"); // Writes users first name using Stream Writer Command
age = Console.ReadLine();
swrNames.WriteLine(age); // This dictates where the age will be stored in the text file
swrNames.Flush();
Console.Clear();
}
swrNames.Close();
String NamesTable;
StreamReader swreNames = File.OpenText("Names.txt");
while ((NamesTable = swreNames.ReadLine()) != null)
{
Console.WriteLine(NamesTable);
}
swreNames.Close();
Console.ReadLine();
FileInfo fleNamesTwo = new FileInfo("NamesTwo.txt");
StreamWriter swrNamesTwo = null;
// ------------ CHECK APPEND TO A FILE --------------
if (fleNames.Exists == true)
{
swrNames = fleNames.AppendText();
}
else // -- Create a new text file
{ // Declare Strings
String Name; // Name
String Surnames; // Surname
String Ages; // Age
swrNamesTwo = fleNamesTwo.CreateText();
Console.Write("Enter Your First Name");
Name = Console.ReadLine();
swrNamesTwo.WriteLine(Name);
swrNamesTwo.Flush();
Console.Write("Enter Your Surname");
Surnames = Console.ReadLine();
swrNamesTwo.WriteLine(Surnames);
swrNamesTwo.Flush();
Console.WriteLine("Enter Your Age");
Ages = Console.ReadLine();
swrNamesTwo.WriteLine(Ages);
swrNamesTwo.Flush();
Console.Clear();
}
Console.Clear();
Console.SetCursorPosition(15, 2);
Console.Write("--- Names Table ---");
Console.SetCursorPosition(10, 4);
Console.Write("First Name");
Console.SetCursorPosition(28, 4);
Console.Write("Surname");
Console.SetCursorPosition(48, 4);
Console.Write("Age");
Console.ReadLine();
do
{
//Read a File a Streamreader Command
swrNames.WriteLine(firstname); // Reads from first name from file Names.txt
Console.SetCursorPosition(10, Counter + 6); // Aligns first name within table settings
swrNames.WriteLine(lastname); // Reads from last name from file Names.txt
Console.SetCursorPosition(28, Counter + 6);
swrNames.WriteLine(age); // Reads from age from file Names.txt
Console.SetCursorPosition(48, Counter + 6);
Console.WriteLine("{0} {1} is {2} years old", firstname, lastname, age);
Console.Clear(); //Clears Screen
Console.ReadLine();
} while ((firstname = swreNames.ReadLine()) != null); //Writes out the input from the text file
}
}
}
好的,所以你乐于助人的人让我克服了错误!谢谢 :)
但是,(总是有一个但是!!!)我仍然无法让流写入器/读取器在表中显示结果。我让表格出现在结果之后,但表格是空白的,除了表格标题都在它们整洁的占位符中!任何人..... :):):)