在不应该输入"If"语句的位置



此代码应监视用户的击键,并在键入错误的字符时停止他。然而,当涉及到应该与字符进行比较的 if 语句时,一切都会出错

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(); 
}

您进入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 语句是真的,"错误的输入!!超过长度"被打印出来。

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

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

最新更新