0

我刚刚用 Visual Studio 2012 完成了一个关于简单学生评分系统的 WPF 程序。

一切正常,它没有显示错误或无效语句(红线),并且有一个提交按钮,但是每当我第二次按下它时,程序就会崩溃。我检查了我的代码几次,我找不到解决方案。所以它可能会很麻烦:S 程序的工作原理是这样的:用户必须输入一些学生才能放成绩。--> 按 OK,当用户输入完成绩后,会出现字段列表 --> 按提交。这些值都输入到 2 个数组中,一个用于名称,另一个用于将转换为字母的等级。将为第二个学生清除字段,依此类推,直到达到学生人数为止。该程序应该给出一个包含名称及其字母等级的消息框。

using System.IO;

namespace Grading_System_Project_3
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public static int nbofstudents;
        public double m1, m2, p1, p2, f;
        public int i = 0;
        public int l;
        public double avg;
        public string name;
        public char fg;
        public char[] gr;
        public string[] names;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (Convert.ToInt32(textBox1.Text) <= 0)
            {
                MessageBox.Show("Please Enter a valid students number");
            }
            else
            {

                nbofstudents = Convert.ToInt32(textBox1.Text);


                StreamWriter SW = File.CreateText(@"C:\grades.txt");
                SW.Close();


                textBox2.Visibility = System.Windows.Visibility.Visible;
                textBox3.Visibility = System.Windows.Visibility.Visible;
                textBox4.Visibility = System.Windows.Visibility.Visible;
                textBox5.Visibility = System.Windows.Visibility.Visible;
                textBox6.Visibility = System.Windows.Visibility.Visible;
                textBox7.Visibility = System.Windows.Visibility.Visible;
                label2.Visibility = System.Windows.Visibility.Visible;
                label3.Visibility = System.Windows.Visibility.Visible;
                label4.Visibility = System.Windows.Visibility.Visible;
                label5.Visibility = System.Windows.Visibility.Visible;
                label6.Visibility = System.Windows.Visibility.Visible;
                label7.Visibility = System.Windows.Visibility.Visible;
            }
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            StreamWriter sw2 = new StreamWriter(@"C:\grades.txt");
                if (i < nbofstudents)
                {
                    name = textBox2.Text;
                    m1 = Convert.ToInt32(textBox3.Text);
                    m2 = Convert.ToInt32(textBox4.Text);
                    p1 = Convert.ToInt32(textBox5.Text);
                    p2 = Convert.ToInt32(textBox6.Text);
                    f = Convert.ToInt32(textBox7.Text);

                    avg = (m1 + m2 + p1 + p2 + f) / 5;

                    if (avg >= 0 && avg <= 59)
                    {
                        fg = 'F';
                    }
                    else if (avg >= 60 && avg <= 69)
                    {
                        fg = 'D';
                    }
                    else if (avg >= 70 && avg <= 79)
                    {
                        fg = 'C';
                    }
                    else if (avg >= 80 && avg <= 89)
                    {
                        fg = 'B';
                    }
                    else if (avg >= 90 && avg <= 100)
                    {
                        fg = 'A';
                    }

                    string temp = Convert.ToString(fg);

                    try
                    {
                        sw2.WriteLine(name + "\t" + temp);
                        sw2.WriteLine();
                        sw2.Close();
                    }
                    catch (IOException ex)
                    {
                        MessageBox.Show(ex.Message);
                    }

                    i++;
                    textBox2.Clear();
                    textBox3.Clear();
                    textBox4.Clear();
                    textBox5.Clear();
                    textBox6.Clear();
                    textBox7.Clear();
                }
                else
                {
                    try
                    {
                        StreamReader rf = new StreamReader(@"C:\grades.txt");
                        string s = "";
                        while ((s = rf.ReadLine()) != null)
                        {
                            MessageBox.Show(rf.ReadLine());
                        }
                        rf.Close();
                    }
                    catch (IOException ex)
                    {
                        Console.WriteLine(ex.Message);
                    }

                }



            }

    }
}
4

0 回答 0