Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#无效令牌';而';类、结构或接口成员声明中(循环不工作时)_C#_Loops_While Loop - Fatal编程技术网

c#无效令牌';而';类、结构或接口成员声明中(循环不工作时)

c#无效令牌';而';类、结构或接口成员声明中(循环不工作时),c#,loops,while-loop,C#,Loops,While Loop,我是c语言的新手,但对c语言非常熟悉,但是这个错误对我来说没有多大意义,因为我特别遵循了上所示的所需语法 使用系统; 使用System.Collections.Generic; 使用系统组件模型; 使用系统数据; 使用系统图; 使用System.Linq; 使用系统文本; 使用System.Windows.Forms; 命名空间Windows窗体应用程序1 { 公共部分类Form1:Form { 整数年=1; 虽然(年你有while直接在类内,但它应该是方法的一部分,你不能直接在类内编写类似wh

我是c语言的新手,但对c语言非常熟悉,但是这个错误对我来说没有多大意义,因为我特别遵循了上所示的所需语法

使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
命名空间Windows窗体应用程序1
{
公共部分类Form1:Form
{
整数年=1;

虽然(年你有
while
直接在类内,但它应该是方法的一部分,你不能直接在类内编写类似
while
的语句

public partial class Form1 : Form
{
    int years = 1;
    while (years <=10) {
   //^^
公共部分类表单1:表单
{
整数年=1;

while(years您的while循环必须在方法中,它位于变量/方法声明区域。将其删除并放置在方法中。

您有一个while(years
当在方法中使用时,您尝试在类级别上使用它。
public partial class Form1 : Form
{
    int years = 1;
    while (years <=10) {
   //^^
public partial class Form1 : Form
{
    int years = 1;
    public void SomeMethod()
    {
       while (years <=10) {
      //rest of your code
    }