0

ToString();ユーザーが動物の名前の1つを言うたびに、プログラムに1、2、3のような1つの数字を追加させようとしています。したがって、カウント表示(スコアボード)に1つの数字を追加します。lblCountユーザーが見られるようにラベル内に表示されます

現時点では、私はすべてのためにこれを持っています

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SpeechLib;
using System.IO;
using System.Speech.Recognition;
using System.Globalization;

namespace SimpleSpeechRecognition
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private SpeechRecognitionEngine recognizer;

        private void Form1_Load(object sender, EventArgs e)
        {

            //speechListBox1.Enabled = true;
            panel1.Enabled = false;
            panel2.Enabled = false;

            pictureBox2.Image = Image.FromFile(@"C:\animals\" + "happy.jpg");
            lblTitle2.Text = "WELCOME";

            pictureBox1.Image = Image.FromFile(@"C:\animals\" + "zoo.jpg");
            lblTitle.Text = "WELCOME";

            speechListBox1.Items.Add("Dog");
            speechListBox1.Items.Add("Elephant");
            speechListBox1.Items.Add("Cat");
            speechListBox1.Items.Add("Bird");
            speechListBox1.Items.Add("Rabbit");

            recognizer = new SpeechRecognitionEngine(new CultureInfo("EN-US"));
            recognizer.SetInputToDefaultAudioDevice();

            Choices choices = new Choices("Dog", "Elephant", "Cat", "Bird", "Rabbit");
            GrammarBuilder m_GrammarBuilder = new GrammarBuilder(choices);
            Grammar m_Speech = new Grammar(m_GrammarBuilder);
            recognizer.LoadGrammar(m_Speech);

            recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
            recognizer.RecognizeAsync(RecognizeMode.Multiple);
        }

        void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
                //

            foreach (RecognizedWordUnit word in e.Result.Words)
            {
                int count = 0;
                count += 1;

                switch (word.Text)

                {
                    case "Dog":
                        pictureBox1.Image = Image.FromFile(@"C:\animals\" + "dog.jpg");
                        this.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
                        lblTitle.TextAlign = ContentAlignment.MiddleCenter;
                        lblTitle.Text = "DOG";
                        lblCount.Text = count.ToString();
                        break;
                    case "Elephant":
                        pictureBox1.Image = Image.FromFile(@"C:\animals\" + "elephant.jpg");
                        this.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
                        lblTitle.TextAlign = ContentAlignment.MiddleCenter;
                        lblTitle.Text = "ELEPHANT";
                        lblCount.Text = count.ToString();
                        break;
                    case "Cat":
                        pictureBox1.Image = Image.FromFile(@"C:\animals\" + "cat.jpg");
                        this.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
                        lblTitle.TextAlign = ContentAlignment.MiddleCenter;
                        lblTitle.Text = "CAT";
                        lblCount.Text = count.ToString();
                        break;
                    case "Bird":
                        pictureBox1.Image = Image.FromFile(@"C:\animals\" + "bird.jpg");
                        this.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
                        lblTitle.TextAlign = ContentAlignment.MiddleCenter;
                        lblTitle.Text = "BIRD";
                        lblCount.Text = count.ToString();
                        break;
                    case "Rabbit":
                        pictureBox1.Image = Image.FromFile(@"C:\animals\" + "rabbit.jpg");
                        this.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
                        lblTitle.TextAlign = ContentAlignment.MiddleCenter;
                        lblTitle.Text = "RABBIT";
                        lblCount.Text = count.ToString();
                        break;
                }
            }
        }

        private void SayPhrase(string PhraseToSay)
        {

            SpeechVoiceSpeakFlags SpFlags = new SpeechVoiceSpeakFlags();
            SpVoice Voice = new SpVoice();
            Voice.Speak(PhraseToSay, SpFlags);

        }

        private void speechListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SayPhrase(speechListBox1.SelectedItems[0].ToString());
            pictureBox1.Refresh();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            panel1.Enabled = true;
            panel2.Enabled = false;
            speechListBox1.Enabled = true;
            pictureBox1.Enabled = true;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            panel1.Enabled = false;
            panel2.Enabled = true;
            speechListBox1.Enabled = false;
            pictureBox1.Enabled = false;
        }
    }
}

これは私がこのレーベルのために取り組んでいる部分です

void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {

            foreach (RecognizedWordUnit word in e.Result.Words)
            {
                int count = 0;
                count += 1;

                switch (word.Text)

                {
                    case "Dog":
                        pictureBox1.Image = Image.FromFile(@"C:\animals\" + "dog.jpg");
                        this.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
                        lblTitle.TextAlign = ContentAlignment.MiddleCenter;
                        lblTitle.Text = "DOG";
                        lblCount.Text = count.ToString();
                        break;
                    case "Elephant":
                        pictureBox1.Image = Image.FromFile(@"C:\animals\" + "elephant.jpg");
                        this.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
                        lblTitle.TextAlign = ContentAlignment.MiddleCenter;
                        lblTitle.Text = "ELEPHANT";
                        lblCount.Text = count.ToString();
                        break;
                    case "Cat":
                        pictureBox1.Image = Image.FromFile(@"C:\animals\" + "cat.jpg");
                        this.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
                        lblTitle.TextAlign = ContentAlignment.MiddleCenter;
                        lblTitle.Text = "CAT";
                        lblCount.Text = count.ToString();
                        break;
                    case "Bird":
                        pictureBox1.Image = Image.FromFile(@"C:\animals\" + "bird.jpg");
                        this.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
                        lblTitle.TextAlign = ContentAlignment.MiddleCenter;
                        lblTitle.Text = "BIRD";
                        lblCount.Text = count.ToString();
                        break;
                    case "Rabbit":
                        pictureBox1.Image = Image.FromFile(@"C:\animals\" + "rabbit.jpg");
                        this.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
                        lblTitle.TextAlign = ContentAlignment.MiddleCenter;
                        lblTitle.Text = "RABBIT";
                        lblCount.Text = count.ToString();
                        break;
                }
            }
        }
4

2 に答える 2

2

私はあなたの問題を完全に理解しているとは思いませんが、かなり明白なエラーのように見えるものはわかります:

        foreach (RecognizedWordUnit word in e.Result.Words)
        {
            int count = 0;
            count += 1;

これにより、ループを通過するたびにカウントが0にリセットされ、すぐに1が追加されるため、ラベルには常に1と表示されます。

イニシャライザをループの外に移動するだけです。

        int count = 0;
        foreach (RecognizedWordUnit word in e.Result.Words)
        {
            count += 1;
于 2013-03-22T20:22:54.243 に答える
0

プログラムの存続期間中の合計カウントを表示する場合は、カウント変数をフォームのメンバーに移動する必要があります。このような:

public partial class Form1 : Form
{  
     private int count = 0;
     // ...
}

次に、イベントハンドラーを次のように変更します。

void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {

        foreach (RecognizedWordUnit word in e.Result.Words)
        {
            count += 1;
于 2013-03-22T20:23:41.227 に答える