C# 无限循环错误和未处理的异常

C# 无限循环错误和未处理的异常,c#,loops,infinite-loop,unhandled-exception,multi-layer,C#,Loops,Infinite Loop,Unhandled Exception,Multi Layer,我在c#做我的实验室工作。我犯了这个错误。我不知道该怎么处理。 我的代码由几个层组成-学生类 数据层 大多数错误发生在用户界面层。我得到的结果可以在屏幕上看到: 如果您能帮助我解决此问题,我将不胜感激。可能在您的业务层第31行出现错误: st[i].Surname = filed_value[1]; 您需要检查此字符串,可能没有字段值[1]。刚刚提交了_值[0],就这样。未处理的异常:索引位于数组的边界之外。在Business.Fill_data,第31行;在第79行的Interface.

我在c#做我的实验室工作。我犯了这个错误。我不知道该怎么处理。 我的代码由几个层组成-学生类

数据层

大多数错误发生在用户界面层。我得到的结果可以在屏幕上看到:


如果您能帮助我解决此问题,我将不胜感激。

可能在您的业务层第31行出现错误:

st[i].Surname = filed_value[1];

您需要检查此字符串,可能没有字段值[1]。刚刚提交了_值[0],就这样。

未处理的异常:索引位于数组的边界之外。在Business.Fill_data,第31行;在第79行的Interface.output处;Program.Main。第19行。无限循环,这意味着每次我按enter键时它都会打印“错误数据”和“失败”。你可以在图像描述中看到。现在这就是我所说的远程调试。@UweKeim的确,Cassie相信我,你需要5分钟时间与知道如何编程的人在一起,这样他们才能指导你。即使你的问题解决了,这里也有很多需要改变的地方。你把这件事弄得太难了。你基本上把我搞糊涂了。。。这使得代码让我的眼睛流血。你有这些层,但用错了。这不是一个无限循环。这是一个索引超出范围的错误。如果您只需调试代码并逐步执行,则很容易确定原因。
   using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

public class Data{
    public string Name;

    public Data()
    {

    }

    public void Create_file(string Name, Student []st)
    {
        FileStream new_file = new FileStream(Name + ".txt", FileMode.Create);

        for (int i = 0; i < st.Length; i++)
        {
            string write_st = st[i].Name + st[i].Surname + st[i].Year + st[i].Country + st[i].ID + st[i].Result;
            byte[] array = Encoding.Default.GetBytes(write_st);
            new_file.Write(array,0,array.Length);
            new_file.Flush();
        }
        new_file.Close();
    }

    public string Open_file(string Name)
    {
        FileStream new_file = File.OpenRead(Name +".txt");

        byte[] array = new byte[new_file.Length];
        new_file.Read(array,0,array.Length);
        string ReadText = Encoding.Default.GetString(array);

        return ReadText;
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class Business{
    public Data d1 = new Data();
    Student[] st;
    public string file_name;
    public string get_data;

    public Business(string file_name, Student [] s)
    {
        d1.Create_file(file_name, s);
    }

    public void Fill_data(string file_name)
    {
        get_data = d1.Open_file(file_name);
        string[] separators = new string[] {"\r\n"}; 
        string[] lines = get_data.Split(separators, StringSplitOptions.RemoveEmptyEntries);

        st = new Student [lines.Length];

        for (int i = 0; i < lines.Length; i++)
        {
            string [] filed_value = lines[i].Split(' ');
            st[i] = new Student();
            st[i].Name = filed_value[0];
            st[i].Surname = filed_value[1];
            st[i].Year = Convert.ToInt32(filed_value[2]);
            st[i].ID = Convert.ToInt32(filed_value[3]);
            st[i].Country = filed_value[4];
            st[i].Result = Convert.ToInt32(filed_value[5]);
            st[i].Gradebook = filed_value[6];
        }
    }

    public int Find_students()
    {
        int count = 0;

        for (int i = 0; i < st.Length; i++)
        {
            if(st[i].Year == 3 && st[i].Country == "Ukraine")
            {
                count++;
                Console.WriteLine(st[i].Name + " " + st[i].Surname);
            }
        }

        return count;
    }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;


class Interface{
    Data d2 = new Data();
    public Student[] st;
    Business b1;
    public string new_filename;
    const string format = "{0,2}{1,10}{2,10}{3,8}{4,12}{5,12}{6,15}";
    string[] pattern = new string[] { @"^[a-zA-Z]+$", @"^[a-zA-Z]+$", @"\d+", @"\d+", "^([0-9]{1})$", @"\d+" ,@"^([0-9]{6})$" };

    public Interface()
    {

    }

    public void input(int number, string new_filename1)
    {
        st = new Student[number];
        string line = string.Format(format, "Name","Surname","Year","ID","Country","Result","Gradebook");
        Console.WriteLine(line);
        Console.WriteLine();

        //string Inp = Console.ReadLine();
        //string[] FileValue = Inp.Split(' ');

        for (int i = 0; i < st.Length; i++)
        {
            //Console.WriteLine(i + 1);
            string new_line = Console.ReadLine();
            string [] filed_value = new_line.Split(' ');

            if (Check(filed_value) == true)
            {
                st[i] = new Student();
                st[i].Name = filed_value[0];
                st[i].Surname = filed_value[1];
                st[i].Year = Convert.ToInt32(filed_value[2]);
                st[i].ID = Convert.ToInt32(filed_value[3]);
                st[i].Country = filed_value[4];
                st[i].Result = Convert.ToInt32(filed_value[5]);
                st[i].Gradebook = filed_value[6];
            }

            else
            {
                //i--;
                Console.WriteLine("failed");
            }
        }

        new_filename = new_filename1;
        b1 = new Business(new_filename,st);

    }

    public bool Check(string [] filed_value)
    {
        for(int j = 0; j < filed_value.Length ; j++)
        {
            Match match = Regex.Match(filed_value[j],pattern[j]);

            if(!match.Success)
            {
                Console.WriteLine("Wrong data" + " " + filed_value[j]);
                return false; 
            }  
        }
        return true;
    }

    public void output()
    {
        b1.Fill_data(this.new_filename);
        Console.WriteLine("The found students number is" + b1.Find_students());
    }

}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lab_2_layers_C_sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Interface new_I = new Interface();
            Console.WriteLine("Enter quantity of students");
            int count = Convert.ToInt32(Console.ReadLine()); 
            Console.WriteLine("Enter the name of the file");
            string Name = Console.ReadLine();
            new_I.input(count,Name);
            new_I.output();

            Console.ReadLine();
        }
    }
}
st[i].Surname = filed_value[1];