对象数组仅将数组的第一个元素重复打印到控制台,而不是打印每个不同的元素

  • 本文关键字:元素 打印 数组 控制台 第一个 对象 c#
  • 更新时间 :
  • 英文 :


目前我是一名学习 c# 的学生,我必须做的作业是创建一个文本文件并为酒店客人写入一些数据,例如

利亚姆,10,0

瑞安,5,1

其格式为

姓名、入住晚数、企业客人(0表示,1为常客(

当我需要读取我已经完成的这个文件但随后读取数据并确定每行上的人是客人还是公司客人并将其存储到对象数组中时,我的问题就出现了。到目前为止,我已经完成了此操作,但是在控制台上打印对象数组时,它只会打印数组的第一个元素 8 次(这是数组的长度(。这是我到目前为止的代码,任何建议都值得赞赏。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using static System.Console;
namespace File_Handling
{
class Program
{
static void Main(string[] args)
{
// Variables to be used in file
int accBalance = 1, NightsStaying = 0;
string name;
char CorporateGuest;

try
{
// Creates file to open and to write to
StreamWriter X = new StreamWriter("Guests.txt");
// File will be written to : File Handlingbindebug
for (int i = 0; i < 8; i++)
{
WriteLine();
Write("What is Guests name? : ");
name = ReadLine();
Write("How many nights are they staying? : ");
NightsStaying = int.Parse(ReadLine());
Write("Corporate Guest? (Y/N) : ");
CorporateGuest = Convert.ToChar(ReadLine());
if (CorporateGuest == 'Y')
{
accBalance = 0;
X.WriteLine($"{name},{NightsStaying},{accBalance}");
}
else
{
X.WriteLine($"{name},{NightsStaying},{accBalance}");
}
}
X.Close();
}
catch (FileNotFoundException )
{
Write("Couldn't find 'Guest.txt' File");
}
FileReader reader = new FileReader();
reader.MainReader();
}
}
public class FileReader
{
object[] arr = new object[8];
public void MainReader()
{
FileStream fs = new FileStream("Guests.txt", FileMode.Open, FileAccess.Read);
StreamReader Input = new StreamReader(fs);
string LineIn;
string[] Fields = new string[4];
LineIn = Input.ReadLine();
while (LineIn != null)
{
int i = 0;
Fields = LineIn.Split(',');
LineIn = Input.ReadLine();
if (Fields[2] != "0")
{
arr[i] = "Guest";
}
else if (Fields[2] == "0")
{
arr[i] = "Corporate Guest";
}
i++;

}
for (int i = 0; i < 8; i++)
{
foreach (object element in arr)
{
if (element != null)
{
WriteLine(element.ToString());
}
}
}

}
}

我认为问题可能出在您的 while 循环上......

int i = 0; // this should be outside the loop
while (LineIn != null)
{
Fields = LineIn.Split(',');
LineIn = Input.ReadLine();
if (Fields[2] != "0")
{
arr[i] = "Guest";
}
else if (Fields[2] == "0")
{
arr[i] = "Corporate Guest";
}
i++;
}

您在 while 循环开始时声明int i = 0,因此循环的每次迭代都会将其重置为 0。尝试在 while 循环之外声明int i = 0

while循环中,每次迭代都将i初始化为 0。这意味着每次将"来宾"或"企业来宾"分配给对象数组时,您只会放入数组的第一个元素。尝试将i声明移动到循环开头的正上方,如下所示:

int i = 0; // This should go first
while (LineIn != null)
{
Fields = LineIn.Split(',');
LineIn = Input.ReadLine();
if (Fields[2] != "0")
{
arr[i] = "Guest";
}
else if (Fields[2] == "0")
{
arr[i] = "Corporate Guest";
}
i++;
}

之后,当您读取文件时,您将有一个嵌套循环。您循环了 8 次,然后在每次迭代中,您遍历数组中的每个元素。尝试只循环一个循环来遍历每个元素:

// No for loop here
foreach (object element in arr)
{
if (element != null)
{
WriteLine(element.ToString());
}
}

我希望这有所帮助!

实际上有两件事应该审查。

  1. 如前面的答案/评论中所述,您在while循环中重新声明(并将初始化为零(索引变量,这导致仅更新第一个(零(数组位置:
int i = 0; // This was extracted from inside the loop. 
while (LineIn != null)
{ 
// Code omitted...
}
    当向控制台显示数组
  1. 时,您将显示数组内容 8 次,因为您有一个从 0 到 8 的for循环计数,然后让foreach循环在数组上交互。您应该只有其中一个循环:
// Either this:
for (int i = 0; i < 8; i++)
{
if (arr[i] != null)
{
WriteLine(arr[i].ToString());
}
}
// ... or ... this:
foreach (object element in arr)
{
if (element != null)
{
WriteLine(element.ToString());
}
}

我希望这有帮助。:)

最新更新