使用方法打印数组时出现命名空间错误

问题描述 投票:0回答:1

我正在尝试打印名为 puzzle 的数组。但是,我收到此错误:

错误 CS8803:顶级语句必须位于命名空间和类型声明之前。

当尝试在最后一行使用 PrettySudoku() 时会发生这种情况。我正在使用 dotnet 和 linux/ubuntu 终端运行它。这是我第一次使用方法。

这是代码:

int[] list1 = new int[]{7, 3, 6, 4, 5, 2, 9, 8, 1};
    int[] list2 = new int[]{1, 9, 8, 6, 3, 7, 4, 5, 2};
    int[] list3 = new int[]{4, 2, 5, 9, 8, 1, 3, 7, 6};
    int[] list4 = new int[]{3, 6, 4, 5, 2, 8, 1, 9, 7};
    int[] list5 = new int[]{9, 5, 2, 7, 1, 4, 6, 3, 8};
    int[] list6 = new int[]{8, 1, 7, 3, 9, 6, 2, 4, 5};
    int[] list7 = new int[]{2, 8, 9, 1, 7, 3, 5, 6, 4};
    int[] list8 = new int[]{6, 7, 3, 2, 4, 5, 8, 1, 9};
    int[] list9 = new int[]{5, 4, 1, 8, 6, 9, 7, 2, 3};

int[][] puzzle = new int[][] {list1, list2, list3, list4, list5, list6, list7, list8, list9};


public class prettyPrinters{

    public void prettySudoku(int[][] arr){

        for(int i = 0; i<arr.GetLength(0); i++){

            for(int j =0; j<arr.GetLength(1); j++){
                Console.WriteLine(arr[i][j]);
            }
        }
    }
}


prettySudoku(puzzle);
c# arrays multidimensional-array namespaces
1个回答
0
投票

错误语句非常清楚,顶级语句(

prettySudoku(puzzle);
)必须位于命名空间(
int [][]
行)和类型声明
public class prettyPrinters
)之前。

这是所有 C(C++、C# 等)语言变体的通用程序结构。

所以,你应该向上移动声明线

prettySudoku(puzzle);
。为避免重复,请阅读此处了解更多详细信息。

© www.soinside.com 2019 - 2024. All rights reserved.