• C#学习笔记(第1周作业)


    受队友诱惑,去听了李强的C#公选课,第二天就完成作业了。

    作业要求:

    1. 小学生加法程序;

    2. 能自由设置难度;

    3. 对结果进行统计。

    第一次写C#程序,遇到不少困难,和队友讨论,百度谷歌一齐上。

    让同学帮忙测试,找出十多处Bug,一一修改。

    本来挺好看的代码,被我改成一坨屎了都。T-T

    上图:

    下载地址:

    http://files.cnblogs.com/haipzm/AdditionGame.zip
    贴上各个窗体和一个公共类的主要代码,程序中还有些小Bug,不过已经不想修改了。
    Setting.cs
      1 using System;
      2 using System.Collections.Generic;
      3 using System.ComponentModel;
      4 using System.Data;
      5 using System.Drawing;
      6 using System.Linq;
      7 using System.Text;
      8 using System.Windows.Forms;
      9 
     10 namespace AdditionGame
     11 {
     12     public partial class Setting : Form
     13     {
     14         public Setting()
     15         {
     16             InitializeComponent();
     17         }
     18 
     19         private void Form1_Load(object sender, EventArgs e)
     20         {
     21             textBox1.Text = "";
     22             textBox2.Text = "";
     23             textBox1.Focus();
     24         }
     25 
     26         private void button1_Click(object sender, EventArgs e)
     27         {
     28             textBox1.Text = "";
     29             textBox2.Text = "";
     30             textBox1.Focus();
     31         }
     32 
     33         private void Form1_Activated(object sender, EventArgs e)
     34         {
     35             textBox1.Text = "";
     36             textBox2.Text = "";
     37             textBox1.Focus();
     38         }
     39 
     40         private void button2_Click(object sender, EventArgs e)
     41         {
     42             if (textBox1.Text == "" || textBox2.Text == "")
     43             {
     44                 Error f7 = new Error(this);
     45                 f7.Show();
     46                 this.Hide();
     47             }
     48             else
     49             {
     50                 bool Flag = true;
     51                 string Str1 = Convert.ToString(textBox1.Text);
     52                 if (Str1.Length > 6)
     53                     Flag = false;
     54                 else
     55                     for (int i = 0; i < Str1.Length; ++i)
     56                         if (Str1[i] < '0' || Str1[i] > '9')
     57                         {
     58                             Flag = false;
     59                             break;
     60                         }
     61                 string Str2 = Convert.ToString(textBox2.Text);
     62                 if (Str2.Length > 6)
     63                     Flag = false;
     64                 else
     65                     for (int i = 0; i < Str2.Length; ++i)
     66                         if (Str2[i] < '0' || Str2[i] > '9')
     67                         {
     68                             Flag = false;
     69                             break;
     70                         }
     71                 if (Flag)
     72                 {
     73                     ClassCommon.Scope = Convert.ToInt32(textBox1.Text);
     74                     ClassCommon.Total = Convert.ToInt32(textBox2.Text);
     75                     if (ClassCommon.Scope == 0 || ClassCommon.Total == 0)
     76                     {
     77                         Error f5 = new Error(this);
     78                         f5.Show();
     79                         this.Hide();
     80                     }
     81                     else
     82                     {
     83                         Playing f2 = new Playing(this);
     84                         f2.Show();
     85                         this.Hide();
     86                     }
     87                 }
     88                 else
     89                 {
     90                     Error f6 = new Error(this);
     91                     f6.Show();
     92                     this.Hide();
     93                 }
     94             }
     95         }
     96 
     97         private void button3_Click(object sender, EventArgs e)
     98         {
     99             this.Close();
    100         }
    101 
    102         private void button4_Click(object sender, EventArgs e)
    103         {
    104             About f3 = new About(this);
    105             f3.Show();
    106             this.Hide();
    107         }
    108 
    109     }
    110 
    111 }

    Playing.cs

      1 using System;
      2 using System.Collections.Generic;
      3 using System.ComponentModel;
      4 using System.Data;
      5 using System.Drawing;
      6 using System.Linq;
      7 using System.Text;
      8 using System.Windows.Forms;
      9 
     10 namespace AdditionGame
     11 {
     12     public partial class Playing : Form
     13     {
     14         Form f1 = new Setting();
     15         public Playing(Setting firstForm)
     16         {
     17             InitializeComponent();
     18             f1 = firstForm;
     19         }
     20 
     21         private void Get_Random_Addend()
     22         {
     23             Random seed = new Random();
     24             Random Addend = new Random(seed.Next());
     25             for (int i = 0; i < ClassCommon.Total; ++i)
     26             {
     27                 ClassCommon.F[i].Addend1 = Addend.Next(ClassCommon.Scope);
     28                 ClassCommon.F[i].Addend2 = Addend.Next(ClassCommon.Scope - ClassCommon.F[i].Addend1);
     29             }
     30         }
     31 
     32         private void Put_Random_Addend()
     33         {
     34             textBox1.Text = Convert.ToString(ClassCommon.F[ClassCommon.Count].Addend1);
     35             textBox2.Text = Convert.ToString(ClassCommon.F[ClassCommon.Count].Addend2);
     36             ++ClassCommon.Count;
     37             textBox3.Focus();
     38         }
     39 
     40         private void Form2_Load(object sender, EventArgs e)
     41         {
     42             progressBar1.Minimum = 0;
     43             progressBar1.Maximum = ClassCommon.Total*10;
     44             progressBar1.Value = 0;
     45             for (int i = 0; i < ClassCommon.Total; ++i)
     46                 ClassCommon.F[i].Ans = -1;
     47             ClassCommon.Count = 0;
     48             Get_Random_Addend();
     49             Put_Random_Addend();
     50             progressBar1.Value += 10;
     51             if (ClassCommon.F[ClassCommon.Count - 1].Ans != -1)
     52                 textBox3.Text = Convert.ToString(ClassCommon.F[ClassCommon.Count - 1].Ans);
     53             else
     54                 textBox3.Text = "";
     55             textBox3.Focus();
     56         }
     57 
     58         private void button3_Click(object sender, EventArgs e)
     59         {
     60             this.Close();
     61             f1.Show();
     62         }
     63 
     64         private void button4_Click(object sender, EventArgs e)
     65         {
     66             this.Close();
     67             f1.Close();
     68         }
     69 
     70         private void Form2_Activated(object sender, EventArgs e)
     71         {
     72             textBox3.Focus();
     73             button1.Enabled = false;
     74             this.Text = "" + Convert.ToString(ClassCommon.Count) + "";
     75             if (ClassCommon.Count == ClassCommon.Total)
     76                 button2.Text = "完成";
     77             else
     78                 button2.Text = "下一题";
     79         }
     80 
     81         private void button2_Click(object sender, EventArgs e)
     82         {
     83             if (textBox3.Text != "" && textBox3.TextLength < 7)
     84             {
     85                 if (ClassCommon.Count == ClassCommon.Total)
     86                 {
     87                     ClassCommon.F[ClassCommon.Count - 1].Ans = Convert.ToInt32(textBox3.Text);
     88                     Result f4 = new Result(this.f1);
     89                     this.Close();
     90                     f4.Show();
     91                 }
     92                 else
     93                 {
     94                     button1.Enabled = true;
     95                     ClassCommon.F[ClassCommon.Count - 1].Ans = Convert.ToInt32(textBox3.Text);
     96                     Put_Random_Addend();
     97                     progressBar1.Value += 10;
     98                 }
     99             }
    100             if (ClassCommon.F[ClassCommon.Count - 1].Ans != -1)
    101                 textBox3.Text = Convert.ToString(ClassCommon.F[ClassCommon.Count - 1].Ans);
    102             else
    103                 textBox3.Text = "";
    104             this.Text = "" + Convert.ToString(ClassCommon.Count) + "";
    105             if (ClassCommon.Count == ClassCommon.Total)
    106                 button2.Text = "完成";
    107             else
    108                 button2.Text = "下一题";
    109             textBox3.Focus();
    110         }
    111 
    112         private void button1_Click(object sender, EventArgs e)
    113         {
    114             ClassCommon.Count -= 2;
    115             if (ClassCommon.Count == 0)
    116                 button1.Enabled = false;
    117             Put_Random_Addend();
    118             progressBar1.Value -= 10;
    119             if (ClassCommon.F[ClassCommon.Count - 1].Ans != -1)
    120                 textBox3.Text = Convert.ToString(ClassCommon.F[ClassCommon.Count - 1].Ans);
    121             else
    122                 textBox3.Text = "";
    123             if (ClassCommon.Count == ClassCommon.Total)
    124                 button2.Text = "完成";
    125             else
    126                 button2.Text = "下一题";
    127             this.Text = "" + Convert.ToString(ClassCommon.Count) + "";
    128             textBox3.Focus();
    129         }
    130 
    131     }
    132 }

    Error.cs

     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Windows.Forms;
     9 
    10 namespace AdditionGame
    11 {
    12     public partial class Error : Form
    13     {
    14         Form f1 = new Setting();
    15         public Error(Form firstForm)
    16         {
    17             InitializeComponent();
    18             f1 = firstForm;
    19         }
    20 
    21         private void Error_Load(object sender, EventArgs e)
    22         {
    23             button4.Focus();
    24         }
    25 
    26         private void button4_Click(object sender, EventArgs e)
    27         {
    28             this.Close();
    29             f1.Show();
    30         }
    31 
    32         private void button3_Click(object sender, EventArgs e)
    33         {
    34             this.Close();
    35             f1.Close();
    36         }
    37     }
    38 }

    Result.cs

     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Windows.Forms;
     9 
    10 namespace AdditionGame
    11 {
    12     public partial class Result : Form
    13     {
    14         Form f1 = new Setting();
    15         public Result(Form firstForm)
    16         {
    17             InitializeComponent();
    18             this.f1 = firstForm;
    19         }
    20 
    21         private void button3_Click(object sender, EventArgs e)
    22         {
    23             this.Close();
    24             f1.Close();
    25         }
    26 
    27         private void Result_Load(object sender, EventArgs e)
    28         {
    29             ClassCommon.Right = 0;
    30             ClassCommon.Wrong = 0;
    31             ClassCommon.Rate = 0.0;
    32             for (int i = 0; i < ClassCommon.Total; ++i)
    33                 if (ClassCommon.F[i].Addend1 + ClassCommon.F[i].Addend2 == ClassCommon.F[i].Ans)
    34                     ++ClassCommon.Right;
    35                 else
    36                     ++ClassCommon.Wrong;
    37             ClassCommon.Rate = Convert.ToDouble(ClassCommon.Right) / Convert.ToDouble(ClassCommon.Total) * 100.0;
    38             string Str3 = String.Format("{0:F}", ClassCommon.Rate);
    39             listBox1.Items.Add("此次练习一共有 " + Convert.ToString(ClassCommon.Total) + " 题,答对 " + Convert.ToString(ClassCommon.Right) + " 题,答错 " + Convert.ToString(ClassCommon.Wrong) + " 题,正确率为 " + Str3 + " %");
    40             if (ClassCommon.Right == ClassCommon.Total)
    41                 listBox1.Items.Add("干得漂亮,你全做对了!优秀是一种习惯,希望你继续保持。");
    42             else
    43                 if (ClassCommon.Rate >= 80.0)
    44                     listBox1.Items.Add("你真棒,快看看哪些题目做错了!期待你更上一层楼。");
    45             else
    46                 if (ClassCommon.Rate >= 60.0)
    47                     listBox1.Items.Add("做得一般,还需要付出更大的努力才行哦!");
    48             else
    49                 listBox1.Items.Add("做得不好!你付出了时间与精力,为什么不付出感情呢?");
    50             if (ClassCommon.Right != ClassCommon.Total)
    51             {
    52                 listBox1.Items.Add("以下是你做错的题目以及正确答案:");
    53                 for (int i = 0; i < ClassCommon.Total; ++i)
    54                     if (ClassCommon.F[i].Addend1 + ClassCommon.F[i].Addend2 != ClassCommon.F[i].Ans)
    55                         listBox1.Items.Add("" + Convert.ToString(i + 1) + " 题," + Convert.ToString(ClassCommon.F[i].Addend1) + " + " + Convert.ToString(ClassCommon.F[i].Addend2) + " = " + Convert.ToString(ClassCommon.F[i].Ans) + "✘→" + Convert.ToString(ClassCommon.F[i].Addend1 + ClassCommon.F[i].Addend2) + "");
    56             }
    57             button4.Focus();
    58         }
    59 
    60         private void button4_Click(object sender, EventArgs e)
    61         {
    62             this.Close();
    63             f1.Show();
    64         }
    65 
    66         private void Result_Activated(object sender, EventArgs e)
    67         {
    68             button4.Focus();
    69         }
    70 
    71         private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    72         {
    73 
    74         }
    75 
    76     }
    77 }

    About.cs

     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Windows.Forms;
     9 
    10 namespace AdditionGame
    11 {
    12     public partial class About : Form
    13     {
    14         Form f1 = new Setting();
    15         public About(Setting firstForm)
    16         {
    17             InitializeComponent();
    18             f1 = firstForm;
    19         }
    20 
    21         private void About_Load(object sender, EventArgs e)
    22         {
    23             button4.Focus();
    24         }
    25 
    26         private void button3_Click(object sender, EventArgs e)
    27         {
    28             f1.Close();
    29             this.Close();
    30         }
    31 
    32         private void button4_Click(object sender, EventArgs e)
    33         {
    34             this.Close();
    35             f1.Show();
    36         }
    37 
    38         private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    39         {
    40             System.Diagnostics.Process.Start("http://www.cnblogs.com/haipzm/");
    41         }
    42 
    43         private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    44         {
    45             System.Diagnostics.Process.Start("haipzm@gmail.com");
    46         }
    47 
    48     }
    49 }

    ClassCommon.cs

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 namespace AdditionGame
     7 {
     8     public struct Data
     9     {
    10         public int Addend1;
    11         public int Addend2;
    12         public int Ans;
    13     }
    14     class ClassCommon
    15     {
    16         public static int MaxTotal = 10000000;
    17         public static Data[] F = new Data[MaxTotal];
    18         public static int Count = 0;
    19         public static double Rate = 0.0;
    20         public static int Right = 0;
    21         public static int Wrong = 0;
    22         public static int Total = 0;
    23         public static int Scope = 0;
    24     }
    25 }
  • 相关阅读:
    字串符相关 split() 字串符分隔 substring() 提取字符串 substr()提取指定数目的字符 parseInt() 函数可解析一个字符串,并返回一个整数。
    表单select相关
    createElement() 创建元素 appendChild()添加元素
    css
    docker基本操作
    redis安装及基本操作
    MongoDB安装 基本操作
    printf输出参数的顺序
    静态库与动态库的制作
    linux下ls、split、readlink、iconv命令
  • 原文地址:https://www.cnblogs.com/haipzm/p/3558462.html
Copyright © 2020-2023  润新知