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

Java操作二维数组生成倍数表格输出异常,求问题定位及解决方法

问题分析
  • 行列参数配置错误:你的预期输出是2行8列的表格,但代码中设置cols=5、rows=4,行列数和需求完全不匹配。
  • 数值计算逻辑错误:当前计算doublearr[i][j]时仅用到了列号j,没有引入行号i做乘数,导致每一行的输出完全相同。按照你的预期结果,第i行(从0开始计数)的数值应该是基础值 * (i+1) * (j+1)。
  • 输出格式化缺失:预期输出中千位以上的数值带逗号分隔符,而当前直接打印int类型不会自动添加千位分隔符,需要额外做输出格式化。
修正后代码
public class Main {
    public static void main(String[] args) {
        // 按预期配置行列数:2行8列
        int cols = 8;
        int rows = 2;
        int baseNumber = 100;
        int[][] doublearr = new int[rows][cols];

        for (int i = 0; i < doublearr.length; i++) {
            for (int j = 0; j < doublearr[i].length; j++) {
                // 计算逻辑加入行号乘数
                doublearr[i][j] = baseNumber * (i + 1) * (j + 1);
                // 格式化输出:占8位宽度,自动添加千位分隔符
                System.out.printf("%,8d", doublearr[i][j]);
            }
            System.out.println();
        }
    }
}
运行输出
100     200     300     400     500     600     700     800
     200     400     600     800   1,000   1,200   1,400   1,600

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 08:24:05