等待用户输入,然后继续进行下一个for语句 [英] Waiting for user input before proceeding with next for statement

查看:79
本文介绍了等待用户输入,然后继续进行下一个for语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,该程序在屏幕上具有字母并随机说一个字母.然后,用户将单击一个字母(无论是对还是错),然后继续进行下一个声明,直到完成所有十个为止.如何使程序暂停,直到用户在继续操作之前在屏幕上选择一个字母为止?字母是孩子们用鼠标点击的屏幕上的按钮.这是我到目前为止的内容:

I am writing a program that has the alphabet on the screen and randomly says a letter. Then the user is to click on a letter (whether right or wrong) and then move on to the next for statement until all ten are done. How do I get the program to pause until the user selects a letter on the screen before moving on? The letters are buttons on the screen that are clicked on with a mouse by the kids. Here is what I have so far:

private void StrtGmBtn_Click(object sender, EventArgs e)
{
    //Set variables
    string correct = "Your right!  Great Job!!";
    string incorrect = "Sorry thats wrong:(";
    int count = 10; //Number of times to play in one game
    char[] pick = new char[count];//Variable for each selection
    for (int i = 0; i < count;)
    {
        pick[i] = GetRndm();
        SayLetter(pick[i]);
        if (LetterChoice == pick[i])
        {
            AnswersTxt.Text = correct;
            i++;
        }
        else
        {
            AnswersTxt.Text = incorrect;
            i++;
        }
    }

}
public static char GetRndm()
{
    // This method returns a random lowercase letter.
    // ... Between 'a' and 'z' inclusize.
    int num = _random.Next(0, 26); // Zero to 25
    char let = (char)('a' + num);

    return let;
}
private void  SayLetter(char a)
{
    //char let = (char)('a' + a);
     SoundPlayer Game = new SoundPlayer
                   (@"http://cafe.bevocal.com/libraries/audio/female1/en_us/alphabet/" + a + ".wav");
     Game.Play();
     System.Threading.Thread.Sleep(1000);
}

感谢您的任何帮助!我是新手,只是想写些东西给我的孩子们玩!

Thanks in advance for any help! I am new and just trying to write something for my kids to play with!

推荐答案

嗨!我已经写了一些代码供您签出.您应该能够创建一个新的Windows窗体项目,并且只需在此代码之后代替生成的类Form1代码即可.

这是我为您写的一个完整解决方案,旨在为您提供一些有关如何使用Winforms项目执行类似操作的想法.

注意:我建议您从传递给声音播放器的URL中下载所有声音文件,以便可以确定在播放它们时它们是否可用.

如果您对代码有任何疑问,请给我评论,我会尽力帮助您.

Hi! I have written some code for you to check out. You should be able to create a new windows forms project and just past this code in place of the class Form1 code that is generated.

This is a complete solution I wrote for you to give you some ideas of how you could do something like this using a winforms project.

NOTE: I would suggest you download all the sound files from the url you are passing to the sound player so you can be sure they are available when you go to play them.

If you have any questions about the code just leave me a comment and I will try to help you out.

public partial class Form1 : Form
{
    Button[] alphaButtons = new Button[26];
    char[] charsToSay = new char[10];
    char letterChoice;
    int charIdx = 0;

    public Form1()
    {
        InitializeComponent();

        int asciiCode = 65;
        for (int i = 0; i < alphaButtons.Length; i++)
        {
            alphaButtons[i] = new Button();
            //add the button control to the form
            this.Controls.Add(alphaButtons[i]);
            alphaButtons[i].Size = new System.Drawing.Size(20, 32);
            int prevIdx = i == 0 ? 0 : i - 1;
            alphaButtons[i].Location = new Point(
                alphaButtons[prevIdx].Location.X + alphaButtons[0].Width, 10);
            alphaButtons[i].Text = Convert.ToChar(asciiCode).ToString();
            alphaButtons[i].Click += new EventHandler(AlphaButtons_Click);
            asciiCode++;
        }

        GetRand();

        letterChoice = charsToSay[charIdx];

        Play();
    }

    private void Play()
    {
        SayLetter();
    }

    private void GetRand()
    {
        // change randomness as desired

        Random rand = new Random();

        for (int i = 0; i < charsToSay.Length; i++)
        {
            charsToSay[i] = Convert.ToChar(rand.Next(65, 90));//ascii chars A - Z
        }
    }

    void AlphaButtons_Click(object sender, EventArgs e)
    {
        Button b = sender as Button;
        char c = Convert.ToChar(b.Text);

        if (c == letterChoice)
        {
            //correct
            MessageBox.Show("Congratulations that is Correct!!!");//test
        }
        else
        {
            //incorrect
            MessageBox.Show("Sorry that is incorrect!");//test
        }

        //you may want to wait a second or two before saying next char
        // i.e. Thread.Sleep(2000);
        //just depends on what you want

        charIdx++;
        if (charIdx < charsToSay.Length)
        {
            letterChoice = charsToSay[charIdx];
            Play();
        }
        else
        {
            // GAME OVER
            MessageBox.Show("GAME OVER!");//test
        }
    }

    private void SayLetter()
    {
        string c = letterChoice.ToString().ToLower();
        SoundPlayer sound = new SoundPlayer(
            @"http://cafe.bevocal.com/libraries/audio/female1/en_us/alphabet/" + c + ".wav");
        sound.Play();
    }
}


Hmmm ...现在,我敢肯定,这里有些人会不同意我的解决方案,但这是您可以做的(忘了您到目前为止所获得的一切):
创建一个包含字母的每个字母的数组.然后创建一个列表< TextBox> [ TextBox.Click事件 [ ^ ]设置为相同监听器.
单击TextBox时,将sender强制转换为TextBox,并使用 IndexOf [^ ] .现在您有了索引,可以使用它来获取数组中的相应字母.
要获得随机字母,您仍然可以使用 Random.Next(0,26) [ ^ ],然后从数组.看看您是否可以对此做任何事情.除了用您所有的TextBox es创建列表之外,编写它应该不难,我认为这将大大减少您的代码行:)
我不确定您需要在原始代码中使用庞大的switch语句.
祝你好运!
Hmmm... Now I am sure there''s some people here that wouldn''t agree with my solution, but here is what you could do (forget everything you got so far):
Create an array containing every letter of the alphabet. Then create a List<TextBox>[^] containing all your TextBoxes in alphabetical order (so TextBoxA first, TextBoxB second, TextBoxC third etc...).
Hook up all TextBox.Click Events[^] to the same listener.
When a TextBox is clicked cast the sender to a TextBox and find its position in the list using IndexOf[^]. Now you got the index you can use this to get the corresponding letter in the array.
To get a random letter you can still use Random.Next(0, 26)[^] and fetch the letter at that specific index from the array. See if you can do anything with this. It shouldn''t be to difficult to write and apart from creating the list with all your TextBoxes I think it will greatly reduce your lines of code :)
I am not sure what you needed the huge switch statement for in your original code.
Good luck!


UI应用程序中没有暂停之类的功能.更确切地说,此类应用程序的主线程永久地处于等待模式,这浪费了零CPU时间.仅当用户尝试使用键盘和鼠标输入内容时,操作系统才会唤醒线程.

这样,问题就没有任何意义,甚至是遥不可及的.根本没有什么可讨论的.

我什至不知道该怎么建议.不幸的是,老实说,我不确定是否能为您提供帮助.

这就是为什么.为了开发软件,即使是最简单的软件,也需要成为软件开发人员和程序员.像
There is no such thing as pause in UI applications. More exactly, the main threads of such application permanently sit in a wait mode wasting zero CPU time. The OS wakes up a thread only when the user tries to input something with a keyboard and a mouse.

In this way, the question does not have any sense, even remotely. There is nothing to discuss at all.

I even don''t know what to advice. Unfortunately, I am not sure if helping you can be possible at all, honestly.

Here is why. For developing software, even the simplest one, you need to be a software developer, a programmer. After you have written the "code" like
private void ZBtn_Click(object sender, EventArgs e)
        {
            playLetterSound('z');
        }

case 6:
                            {
                                SayLetter(pick[6]);
                                pickCount++;
                            }

每遍26次(!),表明您所做的事情与编程直接相反.程序员永远不会尝试重复任何代码两次( http://en.wikipedia.org/wiki/Don%27t_repeat_yourself [ ^ ]).从开始到结束,您正在写的东西完全没有用.即使是简单的懒惰也不能使您不必键入所有内容,这甚至是一个问题.我如何希望任何建议都能提供帮助?

—SA

26 times each(!), you demonstrated that you are doing something directly opposite to programming. A programmer would never try to repeat any code twice (http://en.wikipedia.org/wiki/Don%27t_repeat_yourself[^]). You are writing something totally useless from the very beginning to the very end. Even the simple laziness did not save you from typing all that and even is a question. How can I hope that any advice can help?

—SA


这篇关于等待用户输入,然后继续进行下一个for语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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