C#学习笔记(第1周作业)
海胖子 - 2014/02/23受队友诱惑,去听了李强的C#公选课,第二天就完成作业了。
作业要求:
1. 小学生加法程序;
2. 能自由设置难度;
3. 对结果进行统计。
第一次写C#程序,遇到不少困难,和队友讨论,百度谷歌一齐上。
让同学帮忙测试,找出十多处Bug,一一修改。
本来挺好看的代码,被我改成一坨屎了都。T-T
上图:
贴上各个窗体和一个公共类的主要代码,程序中还有些小Bug,不过已经不想修改了。
Setting.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace AdditionGame { public partial class Setting : Form { public Setting() { InitializeComponent(); } private void Form1_Load( object sender, EventArgs e ) { textBox1.Text = ""; textBox2.Text = ""; textBox1.Focus(); } private void button1_Click( object sender, EventArgs e ) { textBox1.Text = ""; textBox2.Text = ""; textBox1.Focus(); } private void Form1_Activated( object sender, EventArgs e ) { textBox1.Text = ""; textBox2.Text = ""; textBox1.Focus(); } private void button2_Click( object sender, EventArgs e ) { if ( textBox1.Text == "" || textBox2.Text == "" ) { Error f7 = new Error( this ); f7.Show(); this.Hide(); }else { bool Flag = true; string Str1 = Convert.ToString( textBox1.Text ); if ( Str1.Length > 6 ) Flag = false; else for ( int i = 0; i < Str1.Length; ++i ) if ( Str1[i] < '0' || Str1[i] > '9' ) { Flag = false; break; } string Str2 = Convert.ToString( textBox2.Text ); if ( Str2.Length > 6 ) Flag = false; else for ( int i = 0; i < Str2.Length; ++i ) if ( Str2[i] < '0' || Str2[i] > '9' ) { Flag = false; break; } if ( Flag ) { ClassCommon.Scope = Convert.ToInt32( textBox1.Text ); ClassCommon.Total = Convert.ToInt32( textBox2.Text ); if ( ClassCommon.Scope == 0 || ClassCommon.Total == 0 ) { Error f5 = new Error( this ); f5.Show(); this.Hide(); }else { Playing f2 = new Playing( this ); f2.Show(); this.Hide(); } }else { Error f6 = new Error( this ); f6.Show(); this.Hide(); } } } private void button3_Click( object sender, EventArgs e ) { this.Close(); } private void button4_Click( object sender, EventArgs e ) { About f3 = new About( this ); f3.Show(); this.Hide(); } } }
Playing.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace AdditionGame { public partial class Playing : Form { Form f1 = new Setting(); public Playing(Setting firstForm) { InitializeComponent(); f1 = firstForm; } private void Get_Random_Addend() { Random seed = new Random(); Random Addend = new Random(seed.Next()); for (int i = 0; i < ClassCommon.Total; ++i) { ClassCommon.F[i].Addend1 = Addend.Next(ClassCommon.Scope); ClassCommon.F[i].Addend2 = Addend.Next(ClassCommon.Scope - ClassCommon.F[i].Addend1); } } private void Put_Random_Addend() { textBox1.Text = Convert.ToString(ClassCommon.F[ClassCommon.Count].Addend1); textBox2.Text = Convert.ToString(ClassCommon.F[ClassCommon.Count].Addend2); ++ClassCommon.Count; textBox3.Focus(); } private void Form2_Load(object sender, EventArgs e) { progressBar1.Minimum = 0; progressBar1.Maximum = ClassCommon.Total*10; progressBar1.Value = 0; for (int i = 0; i < ClassCommon.Total; ++i) ClassCommon.F[i].Ans = -1; ClassCommon.Count = 0; Get_Random_Addend(); Put_Random_Addend(); progressBar1.Value += 10; if (ClassCommon.F[ClassCommon.Count - 1].Ans != -1) textBox3.Text = Convert.ToString(ClassCommon.F[ClassCommon.Count - 1].Ans); else textBox3.Text = ""; textBox3.Focus(); } private void button3_Click(object sender, EventArgs e) { this.Close(); f1.Show(); } private void button4_Click(object sender, EventArgs e) { this.Close(); f1.Close(); } private void Form2_Activated(object sender, EventArgs e) { textBox3.Focus(); button1.Enabled = false; this.Text = "第" + Convert.ToString(ClassCommon.Count) + "题"; if (ClassCommon.Count == ClassCommon.Total) button2.Text = "完成"; else button2.Text = "下一题"; } private void button2_Click(object sender, EventArgs e) { if (textBox3.Text != "" && textBox3.TextLength < 7) { if (ClassCommon.Count == ClassCommon.Total) { ClassCommon.F[ClassCommon.Count - 1].Ans = Convert.ToInt32(textBox3.Text); Result f4 = new Result(this.f1); this.Close(); f4.Show(); } else { button1.Enabled = true; ClassCommon.F[ClassCommon.Count - 1].Ans = Convert.ToInt32(textBox3.Text); Put_Random_Addend(); progressBar1.Value += 10; } } if (ClassCommon.F[ClassCommon.Count - 1].Ans != -1) textBox3.Text = Convert.ToString(ClassCommon.F[ClassCommon.Count - 1].Ans); else textBox3.Text = ""; this.Text = "第" + Convert.ToString(ClassCommon.Count) + "题"; if (ClassCommon.Count == ClassCommon.Total) button2.Text = "完成"; else button2.Text = "下一题"; textBox3.Focus(); } private void button1_Click(object sender, EventArgs e) { ClassCommon.Count -= 2; if (ClassCommon.Count == 0) button1.Enabled = false; Put_Random_Addend(); progressBar1.Value -= 10; if (ClassCommon.F[ClassCommon.Count - 1].Ans != -1) textBox3.Text = Convert.ToString(ClassCommon.F[ClassCommon.Count - 1].Ans); else textBox3.Text = ""; if (ClassCommon.Count == ClassCommon.Total) button2.Text = "完成"; else button2.Text = "下一题"; this.Text = "第" + Convert.ToString(ClassCommon.Count) + "题"; textBox3.Focus(); } } }
Error.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace AdditionGame { public partial class Error : Form { Form f1 = new Setting(); public Error(Form firstForm) { InitializeComponent(); f1 = firstForm; } private void Error_Load(object sender, EventArgs e) { button4.Focus(); } private void button4_Click(object sender, EventArgs e) { this.Close(); f1.Show(); } private void button3_Click(object sender, EventArgs e) { this.Close(); f1.Close(); } } }
Result.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace AdditionGame { public partial class Result : Form { Form f1 = new Setting(); public Result(Form firstForm) { InitializeComponent(); this.f1 = firstForm; } private void button3_Click(object sender, EventArgs e) { this.Close(); f1.Close(); } private void Result_Load(object sender, EventArgs e) { ClassCommon.Right = 0; ClassCommon.Wrong = 0; ClassCommon.Rate = 0.0; for (int i = 0; i < ClassCommon.Total; ++i) if (ClassCommon.F[i].Addend1 + ClassCommon.F[i].Addend2 == ClassCommon.F[i].Ans) ++ClassCommon.Right; else ++ClassCommon.Wrong; ClassCommon.Rate = Convert.ToDouble(ClassCommon.Right) / Convert.ToDouble(ClassCommon.Total) * 100.0; string Str3 = String.Format("{0:F}", ClassCommon.Rate); listBox1.Items.Add("此次练习一共有 " + Convert.ToString(ClassCommon.Total) + " 题,答对 " + Convert.ToString(ClassCommon.Right) + " 题,答错 " + Convert.ToString(ClassCommon.Wrong) + " 题,正确率为 " + Str3 + " %"); if (ClassCommon.Right == ClassCommon.Total) listBox1.Items.Add("干得漂亮,你全做对了!优秀是一种习惯,希望你继续保持。"); else if (ClassCommon.Rate >= 80.0) listBox1.Items.Add("你真棒,快看看哪些题目做错了!期待你更上一层楼。"); else if (ClassCommon.Rate >= 60.0) listBox1.Items.Add("做得一般,还需要付出更大的努力才行哦!"); else listBox1.Items.Add("做得不好!你付出了时间与精力,为什么不付出感情呢?"); if (ClassCommon.Right != ClassCommon.Total) { listBox1.Items.Add("以下是你做错的题目以及正确答案:"); for (int i = 0; i < ClassCommon.Total; ++i) if (ClassCommon.F[i].Addend1 + ClassCommon.F[i].Addend2 != ClassCommon.F[i].Ans) 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) + "✔"); } button4.Focus(); } private void button4_Click(object sender, EventArgs e) { this.Close(); f1.Show(); } private void Result_Activated(object sender, EventArgs e) { button4.Focus(); } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { } } }
About.cs
ClassCommon.csusing System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace AdditionGame { public partial class About : Form { Form f1 = new Setting(); public About(Setting firstForm) { InitializeComponent(); f1 = firstForm; } private void About_Load(object sender, EventArgs e) { button4.Focus(); } private void button3_Click(object sender, EventArgs e) { f1.Close(); this.Close(); } private void button4_Click(object sender, EventArgs e) { this.Close(); f1.Show(); } private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { System.Diagnostics.Process.Start("http://www.cnblogs.com/haipzm/"); } private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { System.Diagnostics.Process.Start("haipzm@gmail.com"); } } }
下载地址:AdditionGame(Single)using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace AdditionGame { public struct Data { public int Addend1; public int Addend2; public int Ans; } class ClassCommon { public static int MaxTotal = 10000000; public static Data[] F = new Data[MaxTotal]; public static int Count = 0; public static double Rate = 0.0; public static int Right = 0; public static int Wrong = 0; public static int Total = 0; public static int Scope = 0; } }
转载保留版权:http://haipz.com/blog/i/41 - 海胖博客