1

My problem is that I want to put a max number limit where the user would put a max question of 2 like an example and my program would show random question only 2 times but it keeps generating numbers I think my problem is with changing the textbox to int and where to place it I'm new to windows forms the first button generate random questions the second button should do the same but minus a number of question every time the user clicks I tried a lot operationbox is for the user to choose his operation(+,-,*,/) and the range box is the range of the random numbers.

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 WindowsFormsApplication7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            rangebox1.Items.Add("[1,100]");
            rangebox1.Items.Add("[1,500]");
            rangebox1.Items.Add("[1,1000]");
            Operationbox1.Items.Add("+");
            Operationbox1.Items.Add("-");
            Operationbox1.Items.Add("*");
            Operationbox1.Items.Add("/");
        }
      
        int[] Rand(int v)
        {

            Random r = new Random();
        int a = r.Next(1,v);
            int b = r.Next(1,v);
            int[] N = {Math.Max(a,b) , Math.Min(a,b)};
            return N;
        
        }
        void generate()
        {
            int range = 0;
            switch (rangebox1.SelectedIndex)
            { 
                case 0:
                    range = 100;
                    break;
                case 1:
                    range = 500;
                        break;
                case 2:
                    range = 1000;
                        break;
                default:
                    MessageBox.Show("Put a range !");
                    break;
            
            }
            int[] numbers = Rand(range);
            switch (Operationbox1.SelectedIndex)
            { 
                case 0:
                    questionlbl1.Text = string.Format("{0} + {1} =",numbers[0],numbers[1]);
                    break;
                case 1:
                    questionlbl1.Text = string.Format("{0} - {1} =" , numbers[0], numbers[1]);
                    break;
                case 2:
                    questionlbl1.Text = string.Format("{0} * {1} =" , numbers[0], numbers[1]);
                    break;
                case 3:
                    questionlbl1.Text = string.Format("{0} / {1} =" , numbers[0], numbers[1]);
                    break;
                default:
                    MessageBox.Show("Please insert a operation");
                    break;
            }
        
        }
        void numberquest()
        {
            int numofquestionleft = Convert.ToInt32(numofquest1.Text);
            int r = int.Parse(numofquest1.Text);
            if (numofquestionleft > 0) generate();
            numofquestionleft--;

        }

        private void genbutton1_Click(object sender, EventArgs e)
        {
            generate();
            
        }

      
        private void button1_Click(object sender, EventArgs e)
        {
            numberquest();
        }
    }
}
4

1 回答 1

2

As far as I understood your question, you are facing issue with numofquestionleft variable and you want to restrict user to make max two number request. You need to modify your code. The way you are doing will not work. One solution could be,

Declare numofquestionleft as class level.

 public partial class Form1 : Form
{
   int numofquestionleft = 2; //here
    public Form1()
    {

And your numberquest() method would look like

 void numberquest()
    {
        
        if (numofquestionleft > 0)
           {
             generate();
           }
        numofquestionleft--;
        //You can use the textBox to communicate with user about number of turns left 
        //as
        numofquest1.Text ="Request left " +numofquestionleft;

    }

But if you still want to read max request from the textBox then the purpose of restricting users to make only two requests is lost (a user may enter some other number and you need to validate input in that case), moreover, you need to find a way to read data once. Hope it helps. 

于 2020-07-04T04:12:40.483 回答