如何使用带2个按钮的随机数 [英] How Do I Use Random Numbers With 2 Buttons

查看:61
本文介绍了如何使用带2个按钮的随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

制作一个涉及用单选按钮进行数学运算的程序。我失去的部分是我们有2套单选按钮,一个标签,一个文本框和2个按钮。第一个单选按钮设置,问题将是加法,减法或乘法。第二组选择1-12或1-24的范围。基于这两个选项,第一个按钮在适当范围内产生2个随机数的问题,并创建正确格式的数学方程。然后用户解决问题,将答案放在文本框中。最后一个按钮然后检查答案并告诉用户他们的对错。



我开始为添加单选按钮做一个if语句并嵌套一个if else声明范围内的声明。然后我在这个类中创建了变量x,y和z作为整数。 x和y是范围内的随机数,z是它们的加法。然后我复制,粘贴和编辑乘法和减法。所以我的随机变量已设置,计算机已解决问题。但是当我开始编写检查答案按钮时,我创建了一个新类。我的问题是我不能引用那些随机数来检查他们的输入。有关如何执行此操作的任何提示?



可以在此处找到最终项目的副本:http://jbwyatt.com/202/a/a4/a4Math.html [ ^ ]



我也可以忍受代码,如果有人告诉我如何。对不起有任何困惑,这是我在这里的第一篇文章。



编辑:



这是一个例子代码

So were making a program that involves doing math with radio buttons. The part I'm lost on is we have 2 sets of radio buttons, a label, a text box, and 2 buttons. the first radio buttons set up wether the problem will be addition, subtraction, or multiplication. The second set chooses a range of either 1-12 or 1-24. Based on those 2 choices the first button generates a problem with 2 random numbers within the proper range, and creates a math equation of the correct format. The user then solves the problem, placing the answer in the text box. The last button then checks the answer and tells the user if their right or wrong.

I started with doing an if statement for the addition radio button and nested an if else statement inside of it for the range. I then created the variables x, y, and z as integers in this class. x and y are random numbers within the range and z is the addition of them. I then copied, pasted, and edited for multiplication and subtraction. So my random variables are set and the computer has solved the problem. But i create a new class when i start coding for the "Check Answer" Button. My problem is i can't reference those random numbers to check with their input. Any tips on how to do this?

A copy of what the final project is supposed to look like can be found here: http://jbwyatt.com/202/a/a4/a4Math.html[^]

Also i can put up the code if someone tells me how. Sorry for any confusion, this is my first post here.



Here is an example of the code

private void btnNewProblem_Click(object sender, EventArgs e)
       {
           int x, y, z;
           Random rnd = new Random();
           if (radAddition.Checked == true)
           {
               if (radSmallRange.Checked == true)
               {
                   x = rnd.Next(0, 13);
                   y = rnd.Next(0, 13);
               }
               else
               {
                   x = rnd.Next(0, 25);
                   y = rnd.Next(0, 25);
               }
               z = x + y;
               lblProblem.Text = x + " + " + y + " = ";
           }
private void btnCheckAnswer_Click(object sender, EventArgs e)
       {
           try
           {
               int Answer = int.Parse(txtAnswer.Text);
           }
           catch
           {
               MessageBox.Show("Please Enter a Whole Number", "Invalid Entry");
           }


       }





我试图使用两个按钮事件中的z变量。由于处理随机数,我需要在btnNewProblem和btnCheckAnswer中引用变量。我需要获取btnNewProblem中创建的z的值,并在点击btnCheckAnswer时将其与用户输入进行比较



Im trying to use the "z" variable in both button events. And since its dealing with random numbers, I need to reference the variable in both btnNewProblem and btnCheckAnswer. I need to Fetch the value for z created in btnNewProblem and compare it to the users input when btnCheckAnswer is clicked

推荐答案

首先,不要创建新的Random实例每次按下按钮 - 将其移动到私有变量并每次使用该实例 - 您将获得更好的随机数字分布。

First off, don;t create a new Random instance each time they press the button - move that to a private variable and use that instance each time - you will get a better random distribution of numbers that way.
private Random rnd = new Random();
private void btnNewProblem_Click(object sender, EventArgs e)
       {
           int x, y, z;
           if (radAddition.Checked == true)
           {
               if (radSmallRange.Checked == true)
               {
                   x = rnd.Next(0, 13);
...
           }



然后,移动 x y z 也是类级私有变量:


Then, move x, y, and z to also be class level private variables:

private Random rnd = new Random();
private int x = -1;
private int y = -1;
private int z = -1;
private void btnNewProblem_Click(object sender, EventArgs e)
       {
           if (radAddition.Checked == true)
           {
               if (radSmallRange.Checked == true)
               {
                   x = rnd.Next(0, 13);
...
           }

现在您的数字将作为类的一部分保留 - 因此您可以在两种方法中使用它们,并且您将引用相同的值。 (我将它们设置为-1,这样它们就有了一个值,这样你就可以检查当用户点击回答按钮时是否设置了问题)



有意义吗?

Now your numbers are preserved as part of the class - so you can use them in both methods and you will reference the same values. (I set them to -1 both so they have a value, and so that you can check if a problem has been set when the use clicks the "answer" button)

Make sense?


这篇关于如何使用带2个按钮的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆