2012-09-05 70 views
0

我在通过this tutorial时找不到我的错误。C#:该名称在当前上下文中不存在

以下输出:

Warning 1 Possible mistaken empty statement (Line 32) 
Error 2 The name 'i' does not exist in the current context (Line 35) 
Error 3 The name 'i' does not exist in the current context (Line 36) 

是用下面的代码得到:

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; 

namespace Loops 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

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

     private void btnForLoops_Click(object sender, EventArgs e) 
     { 
      int loopStart; 
      int loopEnd; 
      int answer; 
      //store the numbers from the text boxes into the two new variables: 
      loopStart = int.Parse(tbLoopStart.Text); 
      loopEnd = int.Parse(tbLoopEnd.Text); 
      for (int i = loopStart; i <= loopEnd; i++) ; 
      { 
       answer = answer + i; 
       listBox1.Items.Add("i = " + i + "answer = " + answer.ToString()); //the display shows as i=1 answer = 1, i=2 answer = 3, etc... 
      } 
     } 
    } 
} 

回答

6
for (int i = loopStart; i <= loopEnd; i++) ; 
              ^

在端部分号应被删除。 否则,编译器将会看到循环结束,并且您的循环仅存在于循环内部。

因此错误。

+0

谢谢你们,我很感谢你的快速回复。现在我仍然坚持我的'答案'变量:“错误1使用未分配的局部变量'答案' – python4gis

+0

将其更改为'int答案= 0;'而不是'int答案;'该变量未分配,这是触发错误 –

+0

这工作,是啊!!你们乡亲是最好的!!最好的祝愿:) – python4gis

1

您的for -loop后面有一个;

1

;在for循环之后,把它拿出来。

相关问题