如何使用C#和Razor使用MVC 5创建数学测验? [英] How do I create a math quiz with MVC 5 using C# and Razor?

查看:109
本文介绍了如何使用C#和Razor使用MVC 5创建数学测验?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何生成两个随机数,然后将它们放在MVC中的方程式中.然后,用户将通过按是"或否"按钮来确定方程式是否正确.将检查答案以查看其答案是否正确,然后将生成一组新的随机数.

我在网上找到的关闭商品是下面的链接,但是里面还有很多其他东西.
https://code.msdn.microsoft.com/Sample-of-a-simple-Captcha-f8c75c43

谢谢您的帮助.


这是我到目前为止的内容:

型号:

Hi, how do I generate two random numbers, then place them in an equation in MVC. The user will then decide if the equation is correct by pressing a yes or a no button. The answer will be checked to see if its correct, then a new set of random numbers will be generated.

The closes thing that I found on the web is the link below, but there is a lot of extra stuff in it.
https://code.msdn.microsoft.com/Sample-of-a-simple-Captcha-f8c75c43

Thank you for your help.


This is what I have so far:

Model:

public class Equation
{
public int a{get; set;}
public int b{get; set;}
public Awn = a+b;

var Rand = new Random((int)DateTime.Now.Ticks);
int a = rand.Next(10, 99);
int b = rand.Next(0, 9);
var equations = string.Format("{0} + {1} = {}", a, b);
}



控制器:



Controller:

public ActionResult Index()
{
return View();



查看:



View:

@using (Html.BeginForm())
{
@Html.LabelFor(model=>model.equations)
{
    <div class="row">
        <input type="submit" value="Yes"/>
        <input type="submit" value="No"/>
    </div>
}

推荐答案

有几点建议:

Equation类应具有一个包含代码的构造函数(示例中的空行之后).

属性ab设置程序可能应该是私有的.

Awn应该是只读属性.

格式字符串中不能替代{}.

您还需要代码才能收到答案.通常,这是通过使用action参数和[HttpPost]属性来完成的.另外,如果在您的情况下有意义,可以在客户端进行验证.

最后,如果要在服务器上验证答案,则需要记住问题"(或至少是回答者).一种方法是使用隐藏字段.
Well a few suggestions:

Equation class should have a constructor that would contains the code (After the empty line in your example).

Property a and b setters should probably be private.

Awn should be a read-only property.

Not sur abot {} in the format string.

You also need code to receive the answer. Typically, this is done using parameters to the action and [HttpPost] attribute. Alternatively, validation might be done on client side if it make sense in your case.

Finally, you need to remember the "question" (or at least the anser) if you want to validate the answer on the server. One way to do it is to use hidden fields.


让我们说您的方程式是

X ^ 2 + Y ^ 2

然后,您可以有两个变量(可能是整数),并将每个变量设置为随机数. (可能在一定范围内-例如,是否要允许负数)

然后将用户的尝试放到另一个变量中(答案).

然后测试(answer == X * X Y * Y)
Let''s say your equation is

X^2 + Y^2

Then you could have two variables (integers, probably) and set them each to a random number. (probably within certain limits - do you want to allow negative numbers, for example)

Then get the user''s attempt into another variable (answer).

Then test if (answer == X*X Y*Y)


我喜欢下面的链接,但我仍在尝试使其正常工作.

http: //stackstackflow.com/questions/20964992/how-to-validate-the-answer-to-a-random-math-equation-in-asp-net-mvc-forms [
i fond the link below, but I''m still trying to get it to work.

http://stackoverflow.com/questions/20964992/how-to-validate-the-answer-to-a-random-math-equation-in-asp-net-mvc-forms[^]


这篇关于如何使用C#和Razor使用MVC 5创建数学测验?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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