在java中,如何让用户在一个不规则的数组中输入列数,但是每行的列数应该不同

zsbz8rwp  于 2021-07-06  发布在  Java
关注(0)|答案(2)|浏览(278)

在java中,我想创建并打印一个不规则数组,首先用户必须输入行数,然后在每行中用户必须输入数字列,然后在每列中输入他想要的数字,直到达到他为每行输入的数字为止,例如

4 11 22 33 44
2 51 8
6 92 1 3 5 3 99

在第一行,用户输入4,这样他就可以键入四个数字
在第二行,用户输入2,这样他就可以键入两个数字
在第三行,用户输入6,这样他就可以键入四个数字
之后,它应该打印用户为每一行输入的数字和他键入的任何内容(应该打印上面的示例)
但出于某种原因使用我的代码

int rows , col, m=0 , z=0 ;

System.out.println("enter number of rows");
rows=input.nextInt();

while(z<rows){
System.out.println("in each row enter number of coloms and then enter whatever number you want in there");
col=input.nextInt();
z++;
for(int i =0 ; i<col ; i++) {
m=input.nextInt();}
}

int [][] test1 = new int [rows][m];

for(int i =0 ; i<test1.length ; i++) {
for( int j =0 ; j<test1[i].length ; j++)
System.out.print(test1[i][j]+ " ");
System.out.println();}

所有的输出都是0,但是用户输入的第一行数是正确的,所以我对此没有问题
因此,与其有这样的输出

enter number of rows
3
in each row enter number of coloms and then enter whatever number you want in there
4 11 22 33 44 // for example the user will enter these numbers and it will be printed the way he typed it
2 51 8
6 92 1 3 5 3 99

但我得到了这个结果

enter number of rows
3
4 11 22 33 44  // if the user have entered these numbers it will print all of the array zeros depending on the first number in the last row 
2 51 8
6 92 1 3 5 3 99

//  this is what I get
0 0 0 0 0 0 
0 0 0 0 0 0 
0 0 0 0 0 0

我一整天都在寻找解决办法,但什么也没找到,有人知道怎么解决这个问题吗?
很抱歉让你读了这么多

des4xlb0

des4xlb01#

通过使用arraylist而不是int“array”来解决:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("How many rows?");
    int rowsSize = input.nextInt();
    List<List<Integer>> rows = new ArrayList<>();
    for (int i = 0; i < rowsSize; i++) {
        System.out.println("How many columns?");
        int colSize = input.nextInt();
        List<Integer> cols = new ArrayList<>();
        for (int j = 0; j < colSize; j++) {
            System.out.println("Write a number");
            cols.add(input.nextInt());
        }
        rows.add(cols);
    }
    for (List<Integer> row : rows) {
        for (Integer number : row) {
            System.out.print(number);
        }
        System.out.println("");
    }
}

正如alex提到的,array不适合这种动态调整大小。大多数情况下,应该首选arraylist而不是array,除非内存/速度非常关键。
edit:使用2d数组为您的问题编写了一个解决方案,但我的建议是更喜欢list:public class stackoverflowquestion{

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("How many classes?");
    int numberOfClasses = input.nextInt();
    int largestNumberOfMarksInAClass = numberOfClasses;
    int[][] classesAndMarks = new int[numberOfClasses][largestNumberOfMarksInAClass];
    for (int i = 0; i < numberOfClasses; i++) {
        System.out.println("How many marks in class  " + (i + 1) + "?");
        int numberOfMarks = input.nextInt();
        if (numberOfMarks > largestNumberOfMarksInAClass) { //If the new class has an higher number of marks,
            largestNumberOfMarksInAClass = numberOfMarks;   //we have to increase the size of the 2d array
            classesAndMarks = getResizedArray(numberOfClasses, largestNumberOfMarksInAClass, classesAndMarks);
        }
        for (int j = 0; j < numberOfMarks; j++) {
            System.out.println("Please write a mark");
            classesAndMarks[i][j] = input.nextInt();
        }
    }

    int highestAverageIndex = 0;
    double[] averageMarks = averageAllClassesMarks(classesAndMarks);
    System.out.println("Class average");
    for (int i = 0; i < averageMarks.length; i++) {
        System.out.println((i + 1) + " " + averageMarks[i]);
        if (averageMarks[i] > averageMarks[highestAverageIndex]) {
            highestAverageIndex = i;
        }
    }
    System.out.println("The highest average is " + averageMarks[highestAverageIndex] +
            " and it is for class " + (highestAverageIndex + 1));
}

private static int[][] getResizedArray(int numberOfClasses, int largestNumberOfMarksInAClass, int[][] classesAndMarks) {
    int[][] resizedArray = new int[numberOfClasses][largestNumberOfMarksInAClass];
    for (int i = 0; i < classesAndMarks.length; i++) {
        int[] mark = classesAndMarks[i];
        for (int j = 0; j < mark.length; j++) {
            resizedArray[i][j] = mark[j];
        }
    }
    return resizedArray;
}

private static double[] averageAllClassesMarks(int[][] classesAndMarks) {
    double[] averageAllClasses = new double[classesAndMarks.length];
    for (int i = 0; i < classesAndMarks.length; i++) {
        averageAllClasses[i] = averageClassMarks(classesAndMarks[i]);
    }
    return averageAllClasses;
}

private static double averageClassMarks(int[] marks) {
    double sum = 0;
    double numberOfMarks = 0;
    for (int mark : marks) {
        sum += mark;
        if (mark != 0) {
            numberOfMarks++;
        }

    }
    return Math.round((sum / numberOfMarks) * 100.0) / 100.0;
}

}

envsm3lx

envsm3lx2#

问题数组

我认为用数组解决这个问题的方法是

int rows = scann.nextInt();
    int[] grid = new int[rows];
    int counter = 0;
    for(int i = 0; i < rows; i++) {
        System.out.println("How Many Columns?");
            int columns = scann.nextInt();
            int[] columnArray = new int[columns];
            grid[i] = columnArray;
}

但是我们不能将数组赋给int[]数组。
数组将是解决此问题的错误数据结构,因为您有不同长度的列。
这是一个更好的选择。

解决方案字符串

在循环询问行数之前,这是外部for循环的条件。
在循环内部,请求作为内部for循环条件的列数
然后在内部请求实际输入的数字,并用它构建一个结果字符串。

import java.util.Scanner;

public class L {
    public static void main(String[] args) {
        Scanner scann = new Scanner(System.in);
        String result = "";
        System.out.println("How many rows?");
        int rowsSize = scann.nextInt();

        for(int i = 0; i < rowsSize; i++) {
            System.out.println("How Many Columns?");
                int columns = scann.nextInt();

            for(int j = 0; j < columns; j++) {
                System.out.println("Tell a number");
                result += scann.nextInt() + " ";
            }
            result += "\n";
        }
System.out.println(result);     
    }
}

输入/输出

How many rows?
2
How Many Columns?
2
Tell a number
1
Tell a number
2
How Many Columns?
3
Tell a number
1
Tell a number
2
Tell a number
1

// Output
1 2 
1 2 1

相关问题