<Window x:Class="mathGame.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:mathGame"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid Margin="10,8,-10,-7">
<Button x:Name="begin" Content="生成试题并开始计时" Margin="508,71,0,305" Width="179" HorizontalAlignment="Left" Click="begin_Click"/>
<TextBox x:Name="miao" TextWrapping="Wrap" Text="" Margin="542,246,113,152" RenderTransformOrigin="1.212,0.325"/>
<TextBlock HorizontalAlignment="Left" Margin="436,246,0,0" TextWrapping="Wrap" Text="剩余时间为:" VerticalAlignment="Top" Height="36" Width="106" TextAlignment="Center"/>
<GroupBox Name="groupBox" Header="试题" BorderThickness="2" Padding="10 0 10 10" Margin="0,128,447,29">
<StackPanel Width="300" Height="200" Margin="0,0,21,0">
<StackPanel.Resources>
<Style TargetType="Label">
<Setter Property="Width" Value="90"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Margin" Value="0 10 0 0"/>
</Style>
<Style TargetType="TextBox">
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Width" Value="116"/>
<Setter Property="Margin" Value="0 10 10 0"/>
</Style>
</StackPanel.Resources>
<WrapPanel Width="220">
<Label Name="label1" Content="1. 11+22="/>
<TextBox Name="one" Text="33"/>
<Label Name="label2" Content="2. 97-18="/>
<TextBox Name="two"/>
<Label Name="label3" Content="3. 9×7="/>
<TextBox Name="three"/>
<Label Name="label4" Content="4. 8/4="/>
<TextBox Name="four"/>
</WrapPanel>
</StackPanel>
</GroupBox>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace mathGame
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void begin_Click(object sender, RoutedEventArgs e)
{
begin.IsEnabled = false;
int second = 25;
//创建计时器对象
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(1000);//间隔1秒
timer.Start();
Random r = new Random ();
miao.Text = $"{second} 秒";
one.Text = two.Text = three.Text = four.Text = "";
int l11,l12, l21, l22,l31,l32,l41,l42;
label1.Content = string.Format("1、{0}+{1}=",l11= r.Next(1, 10),l12= r.Next(1, 10));
label2.Content = string.Format("2、{0}-{1}=", l21=r.Next(1, 10), l22=r.Next(1, 10));
label3.Content = string.Format("3、{0}×{1}=", l31=r.Next(1, 10),l32= r.Next(1, 10));
do
{
l41 = r.Next(1, 10);
l42 = r.Next(1, 10);
}while(l41%l42==0 && l41>l42);
label4.Content = string.Format($"4、{l41}÷{l42}=" );
timer.Tick += delegate
{
if (second == 0)
{
try
{
if ( (l11 + l12 ==int.Parse(one.Text)) &&
(l21 - l22 == int.Parse(two.Text)) &&
(l31 * l32 == int.Parse(three.Text)) &&
(l41 / l42 == int.Parse(four.Text) ))
{
MessageBox.Show("你赢了!");
timer.Stop();
return;
}
else
{
MessageBox.Show("答案错误,你输了!");
timer.Stop();
return;
}
}
catch
{
MessageBox.Show("你输入的不是数字!");
}
finally
{
begin.IsEnabled = true;
}
}
else
{
second--;
miao.Text = $"{second} 秒";
}
};
}
}
}
C#程序设计实验10 数学测验过关小游戏
猜你喜欢
转载自blog.csdn.net/qq_62480054/article/details/131585638
今日推荐
周排行