关于java和c语言中,变量重名问题

请看下面的两段代码:

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int main()
{
    int n;
    n = 10;
    for(int i = 1; i < 2 ; i++)
    {
        int n;
        n = 5;
        cout << n << endl;
    }
    cout << n << endl;
    return 0;
}

package package11;

public class Main
{
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int n;
        n = 10;
        for(int i = 1; i < 2 ; i++)
        {
            int n;
            n = 5;
            System.out.println(n);
        }
        System.out.println(n);
    }
}

C中,是不会出现错误的,而java中,出现了错误,这是不允许这样命名变量的。

猜你喜欢

转载自www.cnblogs.com/674001396long/p/9279205.html