在非常简单的代码中的类、结构或接口成员声明中的无效标记“while" [英] Invalid token 'while' in class, struct, or interface member declaration in very simple code

查看:24
本文介绍了在非常简单的代码中的类、结构或接口成员声明中的无效标记“while"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定问题是什么,但是当我尝试在代码中使用 while 语句时,我不断收到此错误.

I am not sure what the problem is but I keep receiving this error when I try to use a while statement in my code.

课堂中无效的标记while",结构体或接口成员声明

Invalid token 'while' in class, struct, or interface member declaration

我想使用 while 循环来在语句为真时不断更新某些内容.

I want to use a while loop to have something continuously update while a statement is true.

我的其余代码相当长,但每当我输入语法时:

The rest of my code is rather long but whenever I type in the syntax:

while(a<b)
{
//do whatever i want it to do here
}

它立即给了我编译器错误.不太确定是什么问题.我在 Form1.cs 文件下的 C# windows 应用程序中使用所有其他事件处理程序(用于按钮等)执行此操作.谢谢!

It gives me that compiler error right off the bat. Not quite sure what the problem is. I am doing this in a C# windows application under the Form1.cs file with all the other event handlers (for buttons and such). Thanks!

我不知道循环必须放置在方法中(对 c# 来说相当新),但我尝试了它并且没有返回任何错误.感谢大家的帮助!

I was unaware that loops had to be placed within a method (fairly new to c#), but I tried it and no errors were returned. Thanks for your help everybody!

以前,我只在程序的主类中有循环.

Previously, I just had the loop within the main class of the program.

推荐答案

根据错误,听起来编译器认为此代码是直接在类/结构/接口声明的主体中键入的.语句 while/if/for/etc ... 必须出现在方法中.

Based on the error, it sounds like the compiler thinks this code is typed directly in the body of a class/struct/interface declaration. Statements while/if/for/etc ... must appear with in a method.

尝试将此代码移动到解决问题的方法中.如果它在一个方法中,你可能有一个不匹配的括号问题.

Try moving this code into a method to fix the problem. If it's in a method, you likely have a mismatched brace problem.

这篇关于在非常简单的代码中的类、结构或接口成员声明中的无效标记“while"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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