程序练习-珠心算习题生成

珠心算:目前和为9以内加减法;最多随机7个数字,从第二个数字开始,随机数加前面的数字和不超过9, 差不能小于0; 据此规则得:  0-sum<=a <= 9-sum; 得知待填入数字a的范围。

通过for 遍历, 获得处在这个区间的数字在数组中的index, 以这个index做为 random.next(min,max)  中min 和max的值。 进而获了符合要求的随机数字。

最后在timer中使用: button2.performce; 可以定时的执行button2中的代码。

 

Winform下,代码:

Class1.cs中代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 珠心算
{
   public class myclass
    {
     
            Random randomnumber = new Random();
          public  int[] num = { -9,-8,-7,-6,-5,-4,-3,-2,-1,1,2,3,4,5,6,7,8,9};
      
        int[] num2 = { 1,2,3,4,5,6,7,8,9 };
        string[] symbol = { "+", "-" };
         
          public int getrandomnumber(int a,int b)
            {
           
            return num[randomnumber.Next(a, b)];
            }
        public int getrandomnumberU()
        {
            return num2[randomnumber.Next(0, 9)];
        }
 

        public string getrandomsymbol()
        {
            return symbol[randomnumber.Next(0,2)];
        }

        }

   
}
---------------------------------------------------------------------------------
Form1.cs 中代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 珠心算
{
    public partial class Form1 : Form
    {
        delegate void MyDel();
        MyDel myDel;
     int[] numArr1 = {-9,-8,-7,-6,-5,-4,-3,-2,-1,1,2,3,4,5,6,7,8,9 };
           
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            string[] gamestle = { "2数加减", "3数加减", "4数加减", "5数加减", "6数加减", "7数加减" };
            foreach (var item in gamestle)
            {
            comboBox1.Items.Add(item);
            }
            comboBox1.SelectedIndex = 5;
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
        }
     //button ------------------------------------------------------------------------------------------
        TextBox tx1 = new TextBox();
        TextBox tx2 = new TextBox();
        myclass m = new myclass();
        private void button1_Click(object sender, EventArgs e)
        {
            label7.Visible = true;  
       
        }
        int sum = 0;
        int lowerlimt = 0;
        int upperlimit = 0;
        private void button2_Click(object sender, EventArgs e)
        {
         
            label7.Visible = false;
            for (int n = 1; n < groupBox1.Controls.Count+1; n++) //每次点按钮,清空所有数字
            {
                tx2 = (TextBox)this.Controls.Find("textBox" + n.ToString(), true)[0];
                tx2.Text ="0";
            }
           textBox1.Text = m.getrandomnumberU().ToString();//textbox1 先放置随机的无符号数值;
            for (int i = 2; i < comboBox1.SelectedIndex+3; i++)//后面的textbox,依据条件,逐个放置数字;
            {
                tx1 = (TextBox)this.Controls.Find("textBox" + i.ToString(), true)[0];
               // MessageBox.Show("总循环号 i = " + i);
                sum = 0;
                for (int b = 1; b < comboBox1.SelectedIndex+3; b++) //每一次循环,计算出现有的总和
                {
                    tx2 = (TextBox)this.Controls.Find("textBox" + b.ToString(), true)[0];
                    sum = sum + int.Parse(tx2.Text);
                
                }
                  // MessageBox.Show("计算sum 的循环号 b =计算出的和是= "+sum);
                //获取lowerlimit
                for (int a = 0; a < numArr1.Length; a++) //获得总数值之后;依条件,来计算在数组中,哪些索引号之间的数字可以用于下一个textbox的显示
                {
                    if (numArr1[a]==0-sum)// 需研究,最初 ==0-sum+1 为什么总是出现 lower > upper的情况;需要
                    {
                        lowerlimt = a;
                    //  MessageBox.Show("lowerlimt is " + a);
                    }
                 
                 
                }
                //获取upperLimit
                for (int c = 0; c < numArr1.Length; c++)
                {
                    if (numArr1[c] == 9 - sum)
                    {
                        upperlimit = c;
                     

                  //  MessageBox.Show("upper limit is " + c);
                    }
                }
                tx1.Text = m.getrandomnumber(lowerlimt+1, upperlimit+1).ToString("+#;-#;0");
                // MessageBox.Show("新增随机数i=" + i + "值在tx1中是" + tx1.Text);
                sum = 0;
                for (int b = 1; b < comboBox1.SelectedIndex+3; b++) //每一次循环,计算出现有的总数
                {
                    tx2 = (TextBox)this.Controls.Find("textBox" + b.ToString(), true)[0];
                    sum = sum + int.Parse(tx2.Text);
                    label7.Text = sum.ToString();
                       // MessageBox.Show("计算sum 的循环号 b = " + b+"计算出的和是= "+sum);
                }
                lowerlimt = 0;
                upperlimit = 0;
             
            }
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (int n = 1; n < groupBox1.Controls.Count+1; n++) //每次点按钮,清空所有数字
            {
                tx2 = (TextBox)this.Controls.Find("textBox" + n.ToString(), true)[0];
                tx2.Text = "0";
                if (n>comboBox1.SelectedIndex+2)
                {
                    tx2.Visible = false;
                }
                else
                {
                    tx2.Visible = true;
                }

            }
        }
        private void label7_Click(object sender, EventArgs e)
        {
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
           
            timer1.Enabled = true;
         
            timer1.Interval = 1000;
            button2.PerformClick();
        }
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                timer1.Start();
            }
            else
            {
                timer1.Stop();
            }
        }
    }
}
-------------------------------------
winform设计界面:
posted @ 2020-06-27 08:55  小白沙  阅读(289)  评论(0编辑  收藏  举报