C# 虽然循环在公共静态方法C中的if语句之后结束#
我用循环“while”在公共静态方法中编写了一些代码。但这个循环在“if”语句之后结束,应用程序不会抛出任何异常。代码如下:C# 虽然循环在公共静态方法C中的if语句之后结束#,c#,if-statement,methods,while-loop,public,C#,If Statement,Methods,While Loop,Public,我用循环“while”在公共静态方法中编写了一些代码。但这个循环在“if”语句之后结束,应用程序不会抛出任何异常。代码如下: public static void ShortcutDetect() { ShortkeyIndex = 0; while(ShortkeyIndex < 1000) { File.WriteAllText(@"C:\Users\OEM\Desktop\log.txt", File.ReadAllT
public static void ShortcutDetect()
{
ShortkeyIndex = 0;
while(ShortkeyIndex < 1000)
{
File.WriteAllText(@"C:\Users\OEM\Desktop\log.txt",
File.ReadAllText(@"C:\Users\OEM\Desktop\log.txt") + Convert.ToString(ShortkeyIndex));
if(Program.key.Replace("LShiftKey","Shift")
.Replace("RShiftKey","Shift").Replace("RMenu","Alt")
.Replace("LMenu","Alt").Replace("RControlKey","Ctrl")
.Replace("LControlKey","Ctrl").EndsWith(RawShortkeys[ShortkeyIndex]))
{
MessageBox.Show(RawShortkeys[ShortkeyIndex]);
}
ShortkeyIndex++;
}
}
publicstaticvoidshortcutdetect()
{
ShortkeyIndex=0;
while(ShortkeyIndex<1000)
{
File.writealText(@“C:\Users\OEM\Desktop\log.txt”,
File.ReadAllText(@“C:\Users\OEM\Desktop\log.txt”)+Convert.ToString(ShortkeyIndex));
if(程序键更换(“LSShift键”、“Shift”)
.Replace(“rshift键”、“Shift”).Replace(“RMenu”、“Alt”)
.替换(“LMenu”、“Alt”).替换(“RControlKey”、“Ctrl”)
.Replace(“LControlKey”、“Ctrl”).EndsWith(RawShortkeys[ShortkeyIndex]))
{
Show(RawShortkeys[ShortkeyIndex]);
}
ShortkeyIndex++;
}
}
事先谢谢。让我们正确地实施它:
public static void ShortcutDetect() {
// Take loop independent code out of the loop:
// and, please, format it out:
var source = Program.key
.Replace("LShiftKey", "Shift")
.Replace("RShiftKey", "Shift")
.Replace("RMenu", "Alt")
.Replace("LMenu", "Alt")
.Replace("RControlKey", "Ctrl")
.Replace("LControlKey", "Ctrl");
// Wrong type of loop (while): what is ShortkeyIndex?
// where has it been declared, why 1000?
// Please, have a look how the right loop easy to implement and read
foreach (var item in RawShortkeys) {
// Debug: let's output item onto Console
// Console.WriteLine(item);
// Debug: ...or in the file
// File.AppendAllText()@"C:\Users\OEM\Desktop\log.txt", " " + item);
if (source.EndsWith(item)) // <- put a break point here, inspect item's
MessageBox.Show(item);
}
}
publicstaticvoidshortcutdetect(){
//从循环中取出与循环无关的代码:
//请将其格式化:
var source=Program.key
.更换(“lshift键”、“Shift”)
.更换(“换档键”、“换档”)
.替换(“RMenu”、“Alt”)
.替换(“LMenu”、“Alt”)
.替换(“遥控键”、“Ctrl”)
.替换(“LControlKey”、“Ctrl”);
//循环类型错误(while):什么是ShortkeyIndex?
//在哪里申报的,为什么是1000?
//请看一下正确的循环如何易于实现和阅读
foreach(RawShortkeys中的var项){
//调试:让我们将项输出到控制台
//控制台写入线(项目);
//调试:…或在文件中
//File.AppendAllText()@“C:\Users\OEM\Desktop\log.txt”,“项目+”;
if(source.EndsWith(item))//为什么要结束?1000次迭代将花费很短的时间。您如何得出“此周期在if”语句之后结束”的结论?您如何测试它?您确定只有一个“周期”吗执行了吗?你调试代码了吗?从你发布的内容来看,没有任何迹象表明这一点。@Guy,我真的不知道。如果你注意到,我在日志循环中添加了File.writealText。我在日志文件中只看到了0个索引!@MongZhu,我添加了FileWrite方法,它在日志文件中写入选中的索引。我只收到了0个索引,而不是从0到1000。感谢您的帮助。如果此方法检查RawShortkeys[ShortkeyIndex],我发现我的应用程序将停止在.EndsWith()。@SOCIOPATH:您当前的循环有几个问题:完全不清楚什么是ShortkeyIndex
,在哪里声明;什么是1000
(幻数)它如何与RawShortkeys
集合关联;为什么while
在实际需要foreach
集合时循环