我正在尝试创建一个程序,一次从文本文件中读取一个项目,直到它读取所有项目。每次它向屏幕读取项目时,用户输入1,2或3.根据输入的内容,读取的项目将添加到相应的ArrayList
。完成后,它会打印出三个数组中的所有项目。这对我来说不起作用,似乎总是进入默认的switch语句,在我第一次输入数字之后,它会读出3个而不是1个项目。是的,它很乱,我正在寻找一些方向,但期待更多的学习基本的第一个孩子的反应。
以下是代码:
class SplitCheck
{
IList byte mylist = new IList
static void Main(string[] args)
{
byte guestlist;
ArrayList Guest1 = new ArrayList();
ArrayList Guest2 = new ArrayList();
ArrayList Guest3 = new ArrayList();
Console.WriteLine("Thank you for dining with us.");
// Create a path to the My Documents folder and the file name
string filePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) +
Path.DirectorySeparatorChar + "Food.txt";
// Read the contents of the file using ReadAllLines
Console.WriteLine("Please assign each item a guest number");
string[] contents = File.ReadAllLines(filePath);
foreach (string s in contents)
{
Console.WriteLine(s);
guestlist = (byte)Console.Read();
switch (guestlist)
{
case 1 :
//Add to a guest1 array
Guest1.Add(s);
Console.WriteLine("{0} has been added to Guest1", s);
break;
case 2:
//Add to a guest2 array
Guest2.Add(s);
Console.WriteLine("{0} has been added to Guest2", s);
break;
case 3:
//Add to a guest3 array
Guest3.Add(s);
Console.WriteLine("{0} has been added to Guest3", s);
break;
default:
//Add to default array
Console.WriteLine("Default has been used");
break;
}
}
foreach (object o in Guest1)
{
Console.WriteLine("Guest 1 had {0}", o);
}
foreach (object o in Guest2)
{
Console.WriteLine("Guest 2 had {0}", o);
}
foreach (object o in Guest3)
{
Console.WriteLine("Guest 3 had {0}", o);
}
Console.ReadLine();
//Console.WriteLine("Guest 2 had {0}", Guest2());
//Console.WriteLine("Guest 3 had {0}", Guest3());
//Console.ReadLine();
}
}
答案 0 :(得分:1)
guestlist不应该是byte类型,你想要它的类型为char。试试看,如果你仍然坚持阅读:
提示#2: 输入1,然后在guestList的值上放置一个断点。有什么价值?它是49.这个数字有什么重要意义?提示:想想ASCII。
所以试试这个:
char guestlist;
ArrayList Guest1 = new ArrayList();
ArrayList Guest2 = new ArrayList();
ArrayList Guest3 = new ArrayList();
Console.WriteLine("Thank you for dining with us.");
// Create a path to the My Documents folder and the file name
string filePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocum
Path.DirectorySeparatorChar + "Food.txt";
// Read the contents of the file using ReadAllLines
Console.WriteLine("Please assign each item a guest number");
string[] contents = File.ReadAllLines(filePath);
foreach (string s in contents)
{
Console.WriteLine(s);
guestlist = (char)Console.Read();
switch (guestlist)
{
case '1':
//Add to a guest1 array
Guest1.Add(s);
Console.WriteLine("{0} has been added to Guest1", s);
break;
case '2':
//Add to a guest2 array
Guest2.Add(s);
Console.WriteLine("{0} has been added to Guest2", s);
break;
case '3':
//Add to a guest3 array
Guest3.Add(s);
Console.WriteLine("{0} has been added to Guest3", s);
break;
default:
//Add to default array
Console.WriteLine("Default has been used");
break;
答案 1 :(得分:0)
您的问题在这里
guestlist = (byte)Console.Read();
将其更改为
guestlist = Convert.ToInt32(Console.ReadLine());
您正在将控制台读取数据作为一个字节进行转换,并根据反馈进行更新。
答案 2 :(得分:0)
您可以直接检查Console.ReadKey
的结果,而不是从Console.Read()
解释整数值,只需更新字典而不需要switch语句:
var guestCounts = new Dictionary<int, List<string>>
{
{ 1, new List<string>() },
{ 2, new List<string>() },
{ 3, new List<string>() }
}
// Collect inputs
foreach (string food in File.ReadAllLines(filePath))
{
int guest;
if (int.TryParse(Console.ReadKey().KeyChar.ToString(), out guest) &&
guestCounts.ContainsKey(guest))
{
guestCounts[guest].Add(food);
Console.WriteLine("{0} has been added to Guest{1}", food, guest);
}
else
{
Console.WriteLine("Default has been used.");
}
}
// Output results
foreach (int guest in guestCounts.Keys)
{
foreach (string food in guestCounts[guest])
{
Console.WriteLine("Guest {0} had {1}", guest, food);
}
}
答案 3 :(得分:0)
读入的值是字符的ASCII表示形式,而不是实际字符。 此外,当人按下ENTER键时,Console.Read方法也将在换行符中读取,这将导致循环三次(字符,然后是回车符和换行符)。
我建议使用int.TryParse检查输入是否有效,将输入转换为int类型,最后使用Console.ReadLine消除当人按ENTER时的换行符。
int val;
var success = int.TryParse(Console.ReadLine(), out val);
if (success)
{
switch (val)
{
case 1:
Console.WriteLine("has been added to Guest1");
break;
case 2:
Console.WriteLine("has been added to Guest2");
break;
case 3:
Console.WriteLine("has been added to Guest3");
break;
default:
Console.WriteLine("Default has been used");
break;
}
}
else
{
Console.WriteLine("Invalid value");
}