输入“If”语句,不应该输入

时间:2013-12-24 19:06:26

标签: c# keyboard

此代码应该监视用户的击键并在他输入错误的字符时停止他。但是当涉及if语句时,它应该与charecter相比,一切都出错了

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using System.Collections;
using System.IO;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        void Rest()
        {
            counter = -1;

            txt1.Enabled = true;
            txt2.Enabled = true;
            txt3.Enabled = true;
            txt4.Enabled = false;
            txt5.Enabled = true;
            btn2.Enabled = false;
            btn1.Enabled = true;
            pass = "";
            txt4.Clear();
            Dic.Clear();
            turns = 0;
        }

        string path;
        int counter = -1;
        string pass;
        Dictionary<char, letterInfo> Dic;
        int turns = 0;


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {

                if (counter < pass.Length) { MessageBox.Show("WrongInput(Shorter) !!!  "); Rest(); }
                else
                {
                    turns++;
                    if (turns == Convert.ToInt32(txt1.Text))
                    {
                        MessageBox.Show("Test is Done ");
                        /*Writting files */
                        Rest();
                    }
                }
            }
            counter++;
            if (counter >= pass.Length) { MessageBox.Show("WrongInput !!!exceded length  "); Rest(); }
            if ((char)e.KeyValue != pass[counter]) { MessageBox.Show("WrongInput !!!Wrong charecter  " + ((char)e.KeyValue).ToString() + " " + pass[counter].ToString()); Rest(); }
            if (Dic.ContainsKey((char)e.KeyValue))
            {
                Dic[(char)e.KeyValue].start.Add(DateTime.UtcNow.ToString("HH:mm:ss.fff"));
            }
            else
            {
                Dic.Add((char)e.KeyValue, new letterInfo());
                Dic[(char)e.KeyValue].start.Add(DateTime.UtcNow.ToString("HH:mm:ss.fff"));
            }


        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            Dic[(char)e.KeyValue].end.Add(DateTime.UtcNow.ToString("HH:mm:ss.fff"));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string start = "";
            string end = "";
            string letter = "";
            string all;
            foreach (KeyValuePair<char, letterInfo> pair in Dic)
            {
                start = start + " " + pair.Value.start[0];
                end = end + " " + pair.Value.end[0];
                letter = letter + " " + pair.Key;
            }
            all = letter + "\n" + start + "\n" + end;
            MessageBox.Show(all);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                txt1.Enabled = false;
                txt2.Enabled = false;
                txt3.Enabled = false;
                txt4.Enabled = true;
                txt5.Enabled = false;
                btn2.Enabled = true;
                btn1.Enabled = false;
                pass = txt2.Text;
                path = txt3.Text;
                counter = Convert.ToInt32(txt1.Text);
                Dic = new Dictionary<char, letterInfo>();
                /*   if (!File.Exists(path))
                {
                    MessageBox.Show("Error..File not found");
                    Rest();
                }Code to handle the xls files */

            }
            catch (Exception s)
            { MessageBox.Show(s.Message); }
        }
    }
}

这是出现问题的功能

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        if (counter < pass.Length) 
        { 
            MessageBox.Show("WrongInput(Shorter) !!!  "); Rest(); 
        }
        else 
        {
            turns++;
            if (turns == Convert.ToInt32(txt1.Text))
            {
                MessageBox.Show("Test is Done ");
                /*Writting files */
                Rest();
            } 
        }
    }
    counter++;
    if (counter >= pass.Length) 
    { 
        MessageBox.Show("WrongInput !!!exceded length  "); Rest(); 
    }
    if ((char)e.KeyValue != pass[counter]) 
    { 
        MessageBox.Show("WrongInput !!!Wrong charecter  "+((char)e.KeyValue).ToString()+" "+pass[counter].ToString()); Rest(); 
    }
    if (Dic.ContainsKey((char)e.KeyValue))
    {
        Dic[(char)e.KeyValue].start.Add(DateTime.UtcNow.ToString("HH:mm:ss.fff"));
    }
    else
    {
        Dic.Add((char)e.KeyValue, new letterInfo());
        Dic[(char)e.KeyValue].start.Add(DateTime.UtcNow.ToString("HH:mm:ss.fff"));
    }
}

这些是有问题的if语句

  counter++;
   if (counter >= pass.Length) 
   { 
       MessageBox.Show("WrongInput !!!exceded length  "); Rest(); 
   }
   if ((char)e.KeyValue != pass[counter]) 
   { 
       MessageBox.Show("WrongInput !!!Wrong charecter  "+((char)e.KeyValue).ToString()+" "+pass[counter].ToString()); Rest(); 
   }

1 个答案:

答案 0 :(得分:1)

您输入if (counter >= pass.Length)因为:

counter = -1; //starts at -1
pass = ""; //starts at empty string (length of zero)

在您的代码中,这些变量都没有变化:

counter++; //counter is now 0

counter现在等于pass.Length。所以if语句是真的并且打印了“WrongInput !!! exceded length”。

在单击按钮2之前,您不会设置pass的值。因此,在每个触发的按键事件中,传递值为空。

if ((char)e.KeyValue != pass[counter])中,您试图将输入的密钥与传递中的字符编号进行比较。 pass为空并且计数器递增,直到您按下按钮2.因此e.Keyvalue每次都不等于pass[counter]