想在项目中添加计时器请帮忙 [英] Want to add timer in the project please help

查看:78
本文介绍了想在项目中添加计时器请帮忙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在为小孩子制作教育游戏,其中一些数学问题正在显示哪些答案必须提交并基于得分计算的答案。



现在项目即将出现的问题是Timer。我们必须放一个计时器,即当问题显示时,每个问题的计时器应该开始并显示60秒,如果学生在60秒之前回答问题并输入,则下一个问题应该显示,如果他没有在60秒内回答问题下一个问题应该显示。



请帮助。



谢谢提前。



以下是我到目前为止编写的代码



Hi All,

I am making an education game for small children, in which some mathematical questions are getting display for which answer have to be submit and based on the answer the marks get calculated.

Now the problem which is coming the project is Timer. We have to put a timer i.e when a question get display a timer which is 60 second for each question should start and displayed also and if student answer the question before 60 sec and enter, the next question should get display and if he doesn''t answer the question in 60 sec next question should get display.

Please Help.

Thanks in Advance.

Following is the code which I had written so far

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;

namespace EducationGame
{
    static class EduGame
    {
        static float d;
        static Random r = new Random();
        static int a, b, c, marks;
        static string player;
        static string f;
        public static void addition()
        {
            Console.Clear(); ;
            a = r.Next(10);
            b = r.Next(10);
            Console.WriteLine("What is the sum of" + a + "+" + b);
            try
            {
                c = Convert.ToInt32(Console.ReadLine());
                if (c == (a + b))
                {
                    marks = marks + 10;
                    subtraction();
                }
                else
                    subtraction();
            }
            catch (Exception e)
            {
                Console.WriteLine("You Have not Answer this question. Do You Want to Answer? (y/n)");
                f = Console.ReadLine();
                if (f == "y")
                {
                    addition();
                }
                else if (f == "n")
                {
                    subtraction();
                }
                else if (f == "" || f != "y" || f != "n")
                {
                    Console.WriteLine("wrong selection.");
                    Console.ReadLine();
                    subtraction();
                }
            }
        }
        static void subtraction()
        {
            Console.Clear();
            a = r.Next(10);
            b = r.Next(10);
            Console.WriteLine("What is the subtraction of" + a + "-" + b);
            try
            {
                c = Convert.ToInt32(Console.ReadLine());
                if (c == (a - b))
                {
                    marks = marks + 10;
                    multiplication();
                }
                else
                    multiplication();
            }
            catch (Exception e)
            {
                Console.WriteLine("You Have not Answer this question. Do You Want to Answer? (y/n)");
                f = Console.ReadLine();
                if (f == "y")
                {
                    subtraction();
                }
                else if (f == "n")
                {
                    multiplication();
                }
                else if (f == "" || f != "y" || f != "n")
                {
                    Console.WriteLine("wrong selection.");
                    Console.ReadLine();
                    multiplication();
                }
            }
        }
        static void multiplication()
        {
            Console.Clear();
            a = r.Next(10);
            b = r.Next(10);
            Console.WriteLine("What is the product of" + a + "*" + b);
            try
            {
                c = Convert.ToInt32(Console.ReadLine());
                if (c == (a * b))
                {
                    marks = marks + 10;
                    division();
                }
                else
                    division();
            }
            catch (Exception e)
            {
                Console.WriteLine("You Have not Answer this question. Do You Want to Answer? (y/n)");
                f = Console.ReadLine();
                if (f == "y")
                {
                    multiplication();
                }
                else if (f == "n")
                {
                    division();
                }
                else if (f == "" || f != "y" || f != "n")
                {
                    Console.WriteLine("wrong selection.");
                    Console.ReadLine();
                    division();
                }
            }
        }
        static void division()
        {
            Console.Clear();
            a = r.Next(10);
            b = r.Next(10);
            Console.WriteLine("What is the division of" + a + "/" + b);
            try
            {
                c = Convert.ToInt32(Console.ReadLine());
                if (c == (a / b))
                {
                    marks = marks + 10;
                    UpdateMarks();
                }
                else
                    UpdateMarks();
            }
            catch (Exception e)
            {
                Console.WriteLine("You Have not Answer this question. Do You Want to Answer? (y/n)");
                f = Console.ReadLine();
                if (f == "y")
                {
                    division();
                }
                else if (f == "n")
                {
                    UpdateMarks();
                }
                else if (f == "" || f != "y" || f != "n")
                {
                    Console.WriteLine("wrong selection.");
                    Console.ReadLine();
                    UpdateMarks();
                }
            }
        }

        static void UpdateMarks()
        {
            FileStream fs = new FileStream("D:\\Marks.txt", FileMode.Append, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            sw.Write(player);
            sw.Write("'s Marks is : ");
            sw.WriteLine(Convert.ToInt32(marks));
            sw.WriteLine();
            sw.Flush();
            sw.Close();
            fs.Close();
        }
        static void Main(string[] args)
        {
            Console.Write("Please Enter Your Name: ");
            player = Console.ReadLine();

            addition();
            Console.Clear();
            Console.WriteLine("Your marks is: " + marks);
            Console.ReadLine();
        }
    }
}

推荐答案

这里的一大问题是ReadLine是一个阻塞调用 - 直到用户按下ENTER才会返回 - 所以没有计时器会帮助你那么多,除非你改变你做事的方式。



见这里:< a href =http://stackoverflow.com/questions/57615/how-to-add-a-timeout-to-console-readline> http://stackoverflow.com/questions/57615/how-to-add -a-timeout-to-console-readline [ ^ ] - 它提供了一个很好的解决方案。
The big problem here is that ReadLine is a blocking call - it doesn''t return until the user presses ENTER - so no timer will help you that much unless you change the way you do things.

See here: http://stackoverflow.com/questions/57615/how-to-add-a-timeout-to-console-readline[^] - it provides a good solution.


因为您正在使用控制台所需要的实际上是超时ReadLine,这里有一些代码来实现它。



Since you''re using the console what you need is effectively a timeout on ReadLine, here''s some code to achieve that.

[DllImport("User32.Dll", EntryPoint = "PostMessageA")]
private static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, int lParam);

const int VK_RETURN = 0x0D;
const int WM_KEYDOWN = 0x100;

/// <summary>
/// Performs a Console.ReadLine with a timeout.
/// </summary>
/// <param name="timeoutms">The amount of ms to wait before simulating Enter.</param>
/// <returns></returns>
public static string ReadLine(int timeoutms)
{
    bool usercompleted = false;

    ThreadPool.QueueUserWorkItem((o) =>
    {
        Thread.Sleep(timeoutms);

        if (!usercompleted)
        {
            var hWnd = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
            PostMessage(hWnd, WM_KEYDOWN, VK_RETURN, 0);
        }
    });

    try
    {
        return Console.ReadLine();
    }
    finally
    {
        usercompleted = true;
    }
}





您还需要额外的使用量:





You need additional Usings as well:

using System.Runtime.InteropServices;
using System.Threading;





请注意,如果有人忙于打字,请将他们输入的所有内容发送到超时的重点。



这不是万无一失的,但绝对是处理ReadLine超时的最好方法之一。



这个技巧来自StackOverflow帖子 - 我只是略微调整它以使它更容易使用。您可以在此处阅读原始解决方案和替代方案:

http:// stackoverflow.com/questions/9479573/interrupt-console-readline/9479797 [ ^ ]


这篇关于想在项目中添加计时器请帮忙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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