You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java等差序列方阵生成程序BUG排查求助

等差序列方阵生成程序BUG排查求助

我写了一段Java程序,用来生成由等差序列构成的方阵:序列前两个数由用户输入,后续数字基于前两数的差值(公差)生成。但程序输出不符合预期,比如输入首数2、次位数5、方阵大小3时,实际输出和预期不符。试了好几种修改都没找到BUG,求帮忙排查。

程序代码如下:

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、方阵大小3,预期输出:
    2 5 8
    11 14 17
    20 23 26
    
  • 输入首数3、次位数7、方阵大小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
    

BUG分析

你的代码里有两个核心错误:

  1. 第一行后续元素计算错误:当i==0且j>=2时,你用了first + (j+1)*(second-first),但实际每个位置的元素应该是first + j*(second-first)(j是当前列的索引,从0开始,第j个元素对应j个公差)。比如j=2时,应该是2 + 23=8,而不是2+33=11。
  2. 非第一行元素计算错误:你用了first + (size+j+i-1)*(second-first),这个公式完全不符合等差序列的索引逻辑。正确的逻辑是:整个方阵的元素是按行优先的等差序列,第i行第j列的元素是序列中的第i*size + j个元素(从0开始计数),所以应该是first + (i*size + j)*(second-first)。

修复后的代码

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 commonDiff = second - first; // 提前计算公差,避免重复计算

        // 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] = first + (i * size + j) * commonDiff;
            }
        }

        // 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、3时,输出符合预期:

2 5 8 
11 14 17 
20 23 26 

输入3、7、5时,输出也完全匹配预期结果。

内容的提问来源于stack exchange,提问作者NIMISHAN

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 14:02:39