我在通过this tutorial时无法弄清楚我的错误。
以下输出:
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...
}
}
}
}
答案 0 :(得分:6)
for (int i = loopStart; i <= loopEnd; i++) ;
^
应删除末尾的分号。
否则,编译器会将其视为循环的结尾,而int i
仅存在于循环内。
因此错误。
答案 1 :(得分:1)
在;
循环之后,你有一个for
。
答案 2 :(得分:1)
在for-loop之后,取出它。