将用户输入存储到 ArrayList 中

本文关键字:ArrayList 存储 用户 输入 | 更新日期: 2023-09-27 18:33:42

我正在尝试创建一个程序,该程序一次读取一个项目,直到它读取所有项目。 每次将项目读到屏幕上时,用户都会输入 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();
    }
}

将用户输入存储到 ArrayList 中

来宾列表不应该是字节类型,你想要它的类型是字符。 试试看,如果您仍然卡住,请继续阅读:

提示#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;

您的问题在这里

guestlist = (byte)Console.Read();

将其更改为

guestlist = Convert.ToInt32(Console.ReadLine());

您将控制台读取数据转换为字节,并根据反馈进行更新。

读入的值是字符的 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");
        }

您可以直接检查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);
    }
}