0

在阅读本教程时,我无法弄清楚我的错误。

以下输出:

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...
            }
        }
    }
}
4

3 回答 3

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

末尾的分号应该去掉。否则,编译器会将其视为循环的结束,并且您int i仅存在于循环本身中。

因此错误。

于 2012-09-05T08:46:01.793 回答
1

你有一个;for循环。

于 2012-09-05T08:46:02.800 回答
1

; 在for循环之后,将其取出。

于 2012-09-05T08:46:48.213 回答