Java 程序创建一个包含一系列数字的方阵,其中给出前两个数字

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

编写一个Java程序,创建一个包含一系列数字的方阵,其中给出前两个数字,然后根据前两个数字及其差来填充其余数字。

Eg1:如果第一个数字2第二个数字5,它们之间的差是3,如果矩阵是3X3,那么预期输出是:
2,5,8,
11,14,17
20,23,26

Eg1:如果第一个数字3第二个数字7,它们之间的差是4,如果矩阵大小是5,那么预期输出是:
3,7,11,15,19
23,27,31,35,39
43,47,51,55,59
63,67,71,75,79
83,87,91,95,99

import java.util.Scanner;

public class SquareMatrixWithSeries {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the first integer: ");
        int first = scanner.nextInt();

        System.out.print("Enter the second integer: ");
        int second = scanner.nextInt();

        System.out.print("Enter the size of the square matrix: ");
        int size = scanner.nextInt();

        int[][] matrix = new int[size][size];

        // Fill the matrix with the series of numbers
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                if (i == 0 && j == 0) {
                    matrix[i][j] = first;
                } else if (i == 0 && j == 1) {
                    matrix[i][j] = second;
                } else if (i==0){
                    matrix[i][j] = first + (j+1)*(second-first);
                } else {
                    matrix[i][j] = first + (size+j+i-1)*(second-first);
                }
            }
        }

        // Print the matrix
        System.out.println("Square Matrix with the Series of Numbers:");
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
    }
}

上面代码的输出是:

2 5 11
11 14 17
14 17 20

尝试了很多变体来填充矩阵,但找不到代码中的错误。

java matrix multidimensional-array
3个回答
0
投票

好吧 - 像往常一样 - 让我们从头开始。 您扫描第一个数字,然后扫描第二个数字,计算这两个数字之间的差异,然后创建数字,将差异添加到前一个数字以创建新数字。

当然 - 您可以创建一个方阵,但这在这里不是必需的。只需为 size*size 元素创建一个普通矩阵即可。您只需最后将显示拆分为所需的输出即可。这是我的解决方案:

// -- your inputs stays the same
    Scanner scanner = new Scanner(System.in);

    System.out.print("Enter the first integer: ");
    int first = scanner.nextInt();

    System.out.print("Enter the second integer: ");
    int second = scanner.nextInt();

    System.out.print("Enter the size of the square matrix: ");
    int size = scanner.nextInt();        

// -- the normal matrix and the needed definitions   
    int[] matrix = new int[size * size];
    
    matrix[0] = first;
    matrix[1] = second;
    int difference = second - first;
    
// -- the calculation
    for (int i0=2; i0<matrix.length; i0++) {
        matrix[i0] = matrix[i0-1] + difference;
    }

// -- your output
    System.out.println("Square Matrix with the Series of Numbers:");
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            System.out.print(matrix[i*size + j] + " ");
        }
        System.out.println();
    } 

您可以获得所需的输出,并且计算其他规则也相当容易。 快乐编码。


0
投票

使用行数和列数来获得要乘以的因子:

Scanner scanner = new Scanner(System.in);

    System.out.print("Enter the first integer: ");
    int first = scanner.nextInt();

    System.out.print("Enter the second integer: ");
    int second = scanner.nextInt();

    System.out.print("Enter the size of the square matrix: ");
    int size = scanner.nextInt();

    int[][] matrix = new int[size][size];


    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            matrix[i][j] = first + ((i * size + j) * (second - first));
        }
    }

0
投票

由于元素是连续的,因此您只需跟踪

current
值,并在将元素放入矩阵后通过
difference
更新它。您不需要循环内的
if-else
梯子。

import java.util.Scanner;

public class SquareMatrixWithSeries {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the first integer: ");
        int first = scanner.nextInt();

        System.out.print("Enter the second integer: ");
        int second = scanner.nextInt();

        System.out.print("Enter the size of the square matrix: ");
        int size = scanner.nextInt();

        int[][] matrix = new int[size][size];

        int curr = first; // <-- current value is the first number
        int diff = second - first; // <--- save the difference so that you don't 
                                    // need to calculate every time
        // Fill the matrix with the series of numbers
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                matrix[i][j] = curr; // <--- put the current value in the matrix
                curr += diff; // <--- update the current value by adding the difference
            }
        }

        // Print the matrix
        System.out.println("Square Matrix with the Series of Numbers:");
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
    }
}

希望这有帮助。

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