标签:
设计思路:
用WPF窗体设计,在第一个数和第二个数的文本框中输入数值,单击录题按钮,数值保存在n1,n2文档中,把要做的题都保存完后,单击开始按钮,开始做题,每做完一道题,按Enter键,进入下一题,同时提示回答是否正确。如果在时间内做完题就单击结束按钮,弹出对话框“答题结束” 总计,正确的个数及正确率显示出来。
MainWindow.xaml设计为:
代码为:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Windows; 6 using System.Windows.Controls; 7 using System.Windows.Data; 8 using System.Windows.Documents; 9 using System.Windows.Input; 10 using System.Windows.Media; 11 using System.Windows.Media.Imaging; 12 using System.Windows.Navigation; 13 using System.Windows.Shapes; 14 using System.IO; 15 16 17 18 namespace xiaoshitou 19 { 20 /// <summary> 21 /// MainWindow.xaml 的交互逻辑 22 /// </summary> 23 public partial class MainWindow : Window 24 { 25 public MainWindow() 26 { 27 InitializeComponent(); 28 } 29 public static int Count = 0;//总计的个数 30 public static int right = 0;//正确的个数 31 int n = 0; 32 private void button1_Click(object sender, RoutedEventArgs e) 33 { 34 label4.Content = "+"; 35 36 } 37 38 private void button2_Click(object sender, RoutedEventArgs e) 39 { 40 label4.Content = "-"; 41 42 } 43 44 private void button3_Click(object sender, RoutedEventArgs e) 45 { 46 label4.Content = "-"; 47 ; 48 } 49 50 private void button4_Click(object sender, RoutedEventArgs e) 51 { 52 label4.Content = "-"; 53 54 } 55 56 private void button5_Click(object sender, RoutedEventArgs e) 57 { 58 59 StreamWriter n1 = File.AppendText("n1.txt");//第一个数存入第一文档 60 n1.WriteLine(textBox1.Text); 61 n1.Close(); 62 StreamWriter n2 = File.AppendText("n2.txt");//第二个数存入第二个文档 63 n2.WriteLine(label4.Content); 64 n2.Close(); 65 StreamWriter n3 = File.AppendText("n3.txt");//结果存入第三个文档 66 n3.WriteLine(textBox2.Text); 67 n3.Close(); 68 textBox1.Clear(); 69 textBox2.Clear(); 70 textBox3.Clear(); 71 MessageBox.Show("录题成功"); 72 } 73 74 private void button6_Click(object sender, RoutedEventArgs e) 75 { 76 77 string[] n1 = new string[100]; 78 n1 = File.ReadAllLines("n1.txt");//数值一的文档 79 textBox1.Text = n1[n]; 80 string[] n2 = new string[100]; 81 n2 = File.ReadAllLines("n2.txt"); 82 label4.Content = n2[n]; 83 string[] n3 = new string[100]; 84 n3 = File.ReadAllLines("n3.txt"); 85 textBox2.Text = n3[n]; 86 n++; 87 } 88 89 private void button7_Click(object sender, RoutedEventArgs e) 90 { textBox3.IsEnabled = false; 91 MessageBox.Show("运算结束!"); 92 textBox4.Text = Count.ToString();//题目总数 93 textBox5.Text =right.ToString();//正确的个数 94 textBox6.Text = ((right / (double)(Count)) * 100).ToString() + "%";//正确率 95 } 96 97 98 99 private void textBox3_KeyDown(object sender, KeyEventArgs e) 100 { 101 int a = int.Parse(textBox1.Text); 102 int b = int.Parse(textBox2.Text); 103 Char c = Convert.ToChar(label4.Content); 104 Class1 con = new Class1(); 105 con.js(a, b, c); 106 if (e.Key == Key.Enter) 107 { 108 if (con.y1 == int.Parse(textBox3.Text)) 109 { 110 MessageBox.Show("回答正确!下一题请按开始按钮!"); 111 right++; 112 Count++; 113 } 114 115 else 116 { 117 118 MessageBox.Show("回答错误!下一题请按开始按钮!"); 119 Count++; 120 121 } 122 //清空 123 textBox3.Clear(); 124 textBox1.Clear(); 125 textBox2.Clear(); 126 127 } 128 } 129 } 130 }
Class.cs代码
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace xiaoshitou 7 { 8 class Class1 9 { 10 public int result; 11 public int y1 12 { 13 get 14 { 15 return result; 16 } 17 } 18 19 public int js(int n1, int n2, char mark) 20 { 21 if (mark == ‘+‘) 22 { 23 return result = n1 + n2; 24 } 25 else if (mark == ‘-‘) 26 { 27 return result = n1 - n2; 28 } 29 else if (mark == ‘*‘) 30 { 31 return result = n1 * n2; 32 } 33 else if (mark == ‘/‘) 34 { 35 return result = n1 / n2; 36 } 37 return result; 38 } 39 } 40 }
运行过程的各部分显示为以下图片:
标签:
原文地址:http://www.cnblogs.com/xueyanan/p/5040158.html