Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 阵列输出循环_Java_Loops_Multidimensional Array - Fatal编程技术网

Java 阵列输出循环

Java 阵列输出循环,java,loops,multidimensional-array,Java,Loops,Multidimensional Array,我正在完成一个程序,它显示一个文件中给定的数组,然后给出某些行的总数、平均数和具体总数。我遇到的问题是,数组在终止后突然开始循环 我在EclipseIDE中工作 String fileName = ""; if (args.length == 0) { Scanner sc = new Scanner(System.in); System.out.print("Enter the file name :"); fileName =

我正在完成一个程序,它显示一个文件中给定的数组,然后给出某些行的总数、平均数和具体总数。我遇到的问题是,数组在终止后突然开始循环

我在EclipseIDE中工作

    String fileName = "";
    if (args.length == 0) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the file name :");
        fileName = sc.nextLine();
    } else {

        fileName = args[0];
    }
    File file = new File("C:\\Users\\AlaynaC\\Desktop\\2darray.txt");

    try {
        Scanner fin = new Scanner(file);
        int size = fin.nextInt();
        int[][] arr = new int[size][size];
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                arr[i][j] = fin.nextInt();

                Scanner sc = new Scanner(file);

                // The first line of the file contains the number of rows in the array.
                int noOfRows = Integer.parseInt(sc.nextLine());

                // initialize all with -1
                int array[][] = new int[noOfRows][10];
                for (int o = 0; o < noOfRows; o++) {
                    for (int g = 0; g < array[o].length; g++) {
                        array[o][g] = -1;
                    }
                }
                int index = 0;
                while (sc.hasNextLine()) {
                    String line = sc.nextLine();
                    // Each record in the file corresponds to a row in the array.
                    String numbers[] = line.split(" ");
                    // Now feeding the data into the array.
                    for (int o = 0; o < numbers.length; o++) {
                        array[index][o] = Integer.parseInt(numbers[o]);
                    }
                    index++;
                }
                sc.close();
                // This allows the data to be printed to the console.
                System.out.println("~~Array~~");
                for (int o = 0; o < noOfRows; o++) {
                    for (int g = 0; g < array[o].length; g++) {
                        if (array[o][g] != -1)
                            System.out.print(array[o][g] + " ");
                    }
                    System.out.println();
                }
            }
        }

        System.out.println("Total: " + getTotal(arr));
        System.out.println("Average: " + getAverage(arr));
        System.out.println("Row Total: " + getRowTotal(arr, 3));
        System.out.println("Column Total: " + getColumnTotal(arr, 2));
        System.out.println("Highest in Row:" + highestInRow(arr, 2));
        System.out.println("Lowest in Row:" + lowestInRow(arr, 2));

    } catch (FileNotFoundException e) {
        System.out.println(file.getAbsolutePath() + " is not found!");
    }
}
stringfilename=”“;
如果(args.length==0){
扫描仪sc=新的扫描仪(System.in);
System.out.print(“输入文件名:”);
fileName=sc.nextLine();
}否则{
fileName=args[0];
}
File File=新文件(“C:\\Users\\AlaynaC\\Desktop\\2darray.txt”);
试一试{
扫描仪fin=新扫描仪(文件);
int size=fin.nextInt();
int[]arr=新的int[size][size];
对于(int i=0;i

我需要数组只显示一次。从现在起,一旦输入文件,它就会显示多次。

您所说的“数组一旦终止就突然开始循环”是什么意思?我不清楚你所说的数组循环是什么意思?你能准确描述一下你在输出中看到了什么吗?@sprinter一旦输入文件,它会多次显示数组,我只需要它显示一次。你应该从文件中读取多少个数组?只有一个?@KevinAnderson只有一个所以你有两个大的外部循环,看起来它们准备将
size
行的
size
列读入
arr
。但是,在所有这些内容中,您拥有将一个数组的行和列读入另一个数组的所有逻辑,
array
,然后打印出
array
的内容。你只需要内在的逻辑;这两个外环似乎完全没有必要。