我在通過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...
}
}
}
}
謝謝你們,我很感謝你的快速回復。現在我仍然堅持我的'答案'變量:「錯誤1使用未分配的局部變量'答案' – python4gis
將其更改爲'int答案= 0;'而不是'int答案;'該變量未分配,這是觸發錯誤 –
這工作,是啊!!你們鄉親是最好的!!最好的祝願:) – python4gis