索引超出了数组c#的范围,如何解决?

时间:2017-03-28 11:20:34

标签: c# console-application

我有一个显示信息的代码,一切都运行良好,但是当我使用while语句时会出现问题。当我再次重复该过程时,会出现错误消息(因此错误来自while进程)。

错误图片:

Error Picture

这是我的代码:

int u = 0;
int i = 0;
public  void cs4()
    {
        Console.Clear();
        while (Repeat == "Y" || Repeat == "y")
        try
        {
            f = new FileStream(@"E:\Data.txt", FileMode.Open, FileAccess.Read);
            r = new StreamReader(f);
            int VNN = 0;
            while (VNN == 0)
            {
                try
                {
                    Console.WriteLine("\t\t    Here's the current information of Product");
                    case1 q = new case1();
                    q.cs1();
                    Console.WriteLine("\t\t   To update product information, type the Product ID");
                    Console.Write("Type the Product ID\t\t\t: ");
                    nuggetid[i] = Console.ReadLine();
                    if (nuggetid[i].Length == 0)
                    {
                        Console.WriteLine("Sorry you cannot enter minus value!");
                        VNN = 0;
                    }
                    else
                    {
                        VNN = 1;
                    }
                }
                catch (FormatException)
                {
                    Console.WriteLine("Invalid Format");
                    VNN = 0;
                }
            }
            Boolean find = false;
            String Str;
            String Chkstr1;
            int Pos;
            info[0] = "Product ID\t";
            info[1] = "Product Name\t";
            info[2] = "Product Price";
            info[3] = "Current stock";
            f1 = new FileStream(@"E:\Info.txt", FileMode.Append, FileAccess.Write);
            w = new StreamWriter(f1);
            String alltext = "";
            while ((Str = r.ReadLine()) != null)
            {
                Chkstr1 = nuggetid[i].ToString();
                Pos = Str.IndexOf("#");
                String Chkstr2 = Str.Substring(0, Pos);
                String newLine = "";
                if ((Chkstr1.CompareTo(Chkstr2) == 0))
                {
                    String[] elemen = Str.Split('#');
                    Console.WriteLine("\t\t    Here's the detail informations of related product");
                    for (int a = 0; a < 4; a++)
                    {
                        string s = info[u];
                        Console.WriteLine((a + 1) + ". " + s + "\t: " + elemen[a]);
                        u++;
                    }
                    Console.Write("Type the number to edit data [1-4]\t: ");
                    find = true;
                    int indexYangdiUbah = Convert.ToInt32(Console.ReadLine());
                    if (indexYangdiUbah == 1)
                    {
                        Console.WriteLine("The current product ID\t\t\t: " + Chkstr2);
                        Console.Write("Type the new product ID\t\t\t: ");
                        elemen[indexYangdiUbah - 1] = Console.ReadLine();
                    }
                    if (indexYangdiUbah == 2)
                    {
                        Console.WriteLine("The product ID\t\t\t\t: " + Chkstr2);
                        Console.Write("Type the new product name\t\t: ");
                        elemen[indexYangdiUbah - 1] = Console.ReadLine();
                    }
                    if (indexYangdiUbah == 3)
                    {
                        Console.WriteLine("The product ID\t\t\t\t: " + Chkstr2);
                        Console.Write("Type new product price\t\t\t: ");
                        elemen[indexYangdiUbah - 1] = Console.ReadLine();
                    }
                    if (indexYangdiUbah == 4)
                    {
                        Console.WriteLine("The product ID\t\t\t\t: " + Chkstr2);
                        Console.WriteLine("The old stock is\t\t\t: " + elemen[indexYangdiUbah - 1]);
                        Console.Write("Type the new quantity stock do want\t: ");
                        elemen[indexYangdiUbah - 1] = Console.ReadLine();
                    }
                    newLine = elemen[0];
                    for (int a = 1; a < elemen.Length; a++)
                    {
                        newLine = newLine + "#" + elemen[a];
                    }
                    alltext += newLine + "\n";
                    Console.WriteLine("\nThe data has been successfully changed!\n");
                }
                else
                {
                    alltext = alltext + Str + "\n";
                }
            }
            if (!find)
            {
                Console.WriteLine("Sorry, the ID not found!");
            }
            w.Close();
            r.Close();
            File.WriteAllText(@"E:\Data.txt", alltext);
            Console.Write("Do you want update another data? [y-n]\t: ");
            Repeat = Console.ReadLine();
        }
        catch (FileNotFoundException)
        {
            Console.WriteLine("Sorry the data is not found!");
            Console.WriteLine("Make sure the file is exist!");
        }

0 个答案:

没有答案