关于c#:编译时错误“无法在此范围内声明名为’xo’的局部变量……”


Compiletime error “A local variable named 'xo' cannot be declared in this scope…”

编译时,我得到以下错误:

A local variable named 'x0' cannot be declared in this scope because it would give a different meaning to 'x0' , which is already used in 'parent or current' scope do denote something else.

我能做些什么来解决这个问题?

代码是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Text;
 using System.Windows.Forms;

 namespace WindowsApplication1
 {
  public partial class Form1 : Form
  {
    float d1, d2, d3, d4, eta;
    float y1, y2, y3, y4;
    float w10, w11, w12;
    float w20, w21, w22;
    float w30, w31, w32;
    float w40, w41, w42;
    float x10, x11, x12;
    float x20, x21, x22;
    float x30, x31, x32;
    float x40, x41, x42;

    float net, net1, net2, net3, net4;

    float dw10, dw11, dw12;
    float dw20, dw21, dw22;
    float dw30, dw31, dw32;
    float dw40, dw41, dw42;

    public Form1()
    {
        InitializeComponent();
    }
    public void calculation(float x0, float x1, float x2,
                        float w0, float w1, float w2,
                        float d, float net, float y,
                        float dw0, float dw1, float dw2, float eta)

    {
      net = x0 * w0 + x1 * w1 + x2 * w2;

        if (net <= 0)
            y = 1;
        else
            y = -1;

        dw0 = eta * x0 * (d - net);
        dw1 = eta * x1 * (d - net);
        dw2 = eta * x2 * (d - net);

        string a ="{0:f2}   {1:f2}   {2:f2}   {3:f2}   {4:f2}    {5:f2}   {6:f2}   {7:f2}   {8:f2}    {9:f2}     {10:f2}    {11:f2}", x0, x1, x2, w0, w1, w2, d, net, y, dw0, dw1, dw2;
        listBox1.Items.Add(a);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        eta = Convert.ToSingle(textBox20);

        d1 = Convert.ToSingle(textBox19);
        d2 = Convert.ToSingle(textBox18);
        d3 = Convert.ToSingle(textBox17);
        d4 = Convert.ToSingle(textBox16);

        w10 = Convert.ToSingle(textBox3);
        w11 = Convert.ToSingle(textBox2);
        w12 = Convert.ToSingle(textBox1);
        int passes = 0;

        x10 = Convert.ToSingle(textBox7);
        x11 = Convert.ToSingle(textBox11);
        x12 = Convert.ToSingle(textBox15);
        x20 = Convert.ToSingle(textBox6);
        x21 = Convert.ToSingle(textBox10);
        x22 = Convert.ToSingle(textBox14);
        x30 = Convert.ToSingle(textBox5);
        x31 = Convert.ToSingle(textBox9);
        x32 = Convert.ToSingle(textBox13);
        x40 = Convert.ToSingle(textBox4);
        x41 = Convert.ToSingle(textBox8);
        x42 = Convert.ToSingle(textBox12);

        while (passes <= 100)
        {
            calculation(x10, x11, x12, w10, w11, w12, d1, net1, y1, dw10, dw11, dw12, eta);

            w20 = w10 + dw10;
            w21 = w11 + dw11;
            w22 = w12 + dw12;

            calculation(x20, x21, x22, w20, w21, w22, d2, net2, y2, dw20, dw21, dw22, eta);

            w30 = w20 + dw20;
            w31 = w21 + dw21;
            w32 = w22 + dw22;

            calculation(x30, x31, x32, w30, w31, w32, d3, net3, y3, dw30, dw31, dw32, eta);

            w40 = w30 + dw30;
            w41 = w31 + dw31;
            w42 = w32 + dw32;

            calculation(x40, x41, x42, w40, w41, w42, d4, net4, y4, dw40, dw41, dw42, eta);

            w10 = w40 + dw40;
            w11 = w41 + dw41;
            w12 = w42 + dw42;

            passes += 1;
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

}

}


换行

1
string a ="{0:f2}   {1:f2}   {2:f2}   {3:f2}   {4:f2}    {5:f2}   {6:f2}   {7:f2}   {8:f2}    {9:f2}     {10:f2}    {11:f2}", x0, x1, x2, w0, w1, w2, d, net, y, dw0, dw1, dw2;

1
string a = String.Format ("{0:f2}   {1:f2}   {2:f2}   {3:f2}   {4:f2}    {5:f2}   {6:f2}   {7:f2}   {8:f2}    {9:f2}     {10:f2}    {11:f2}", x0, x1, x2, w0, w1, w2, d, net, y, dw0, dw1, dw2);

其他要点:

你对变量的命名是非常不直观的,我认为这违背了良好的实践!

代码中没有xo—您提到的错误是因为x0第二次声明而不是被使用。

要了解有关String.Format的更多信息,请参阅此msdn链接。