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

如何使用JOptionPane修复乘法表程序的输出异常问题?

Fixing Your Multiplication Table Program

Hey there! Let's break down why you're seeing that confusing [I@3d494fbf output instead of the neat multiplication sequence you want, and fix it step by step.

The Core Issues

You've got two small but important problems here:

  1. Printing the array object, not its elements: When you call System.out.println(table);, you're printing the array itself rather than the values stored inside it. Java's array type uses a default toString() method that returns [I@<hashcode> (where [I means "int array" and the hashcode is a unique identifier for the object).
  2. Off-by-one error in multiplication: Your loop uses num*i where i starts at 0—this will give you a 0 as the first element, but your example expects the sequence to start at the multiplier itself (like 2 instead of 0 for input 2).

Fixes and Modified Code

Here are a few straightforward ways to get your desired output:

Option 1: Loop through the array and print elements manually

This gives you full control over formatting (like adding commas between numbers):

import java.util.Scanner;
import javax.swing.JOptionPane;

public class MultiplicationTable {
    public static void main(String[] args) {
        // Note: You aren't using this Scanner object—feel free to remove it if you don't need it!
        Scanner sc = new Scanner(System.in);
        int num = Integer.parseInt(JOptionPane.showInputDialog("Enter a number."));
        int value = Integer.parseInt(JOptionPane.showInputDialog("Enter number of times."));
        printTable(num, value);
    }

    private static void printTable(int num, int value) {
        int[] table = new int[value];
        for(int i = 0; i <= value-1; i++) {
            table[i] = num * (i + 1); // Use i+1 to start from the multiplier itself
        }
        // Print each element with commas
        for(int i = 0; i < table.length; i++) {
            if(i > 0) {
                System.out.print(", ");
            }
            System.out.print(table[i]);
        }
        System.out.println(); // Add a newline at the end
    }
}

Option 2: Use Arrays.toString() for simpler printing

Java's Arrays class has a built-in method to convert arrays to readable strings—this cuts down on manual looping:

import java.util.Scanner;
import javax.swing.JOptionPane;
import java.util.Arrays; // Don't forget to import this!

public class MultiplicationTable {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = Integer.parseInt(JOptionPane.showInputDialog("Enter a number."));
        int value = Integer.parseInt(JOptionPane.showInputDialog("Enter number of times."));
        printTable(num, value);
    }

    private static void printTable(int num, int value) {
        int[] table = new int[value];
        for(int i = 0; i <= value-1; i++) {
            table[i] = num * (i + 1);
        }
        // Convert array to string and remove square brackets if needed
        String tableString = Arrays.toString(table).replace("[", "").replace("]", "");
        System.out.println(tableString);
    }
}

Option 3: Skip the array entirely (simplest approach)

You don't even need to store values in an array—just print them as you calculate:

import java.util.Scanner;
import javax.swing.JOptionPane;

public class MultiplicationTable {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = Integer.parseInt(JOptionPane.showInputDialog("Enter a number."));
        int value = Integer.parseInt(JOptionPane.showInputDialog("Enter number of times."));
        printTable(num, value);
    }

    private static void printTable(int num, int value) {
        for(int i = 1; i <= value; i++) {
            if(i > 1) {
                System.out.print(", ");
            }
            System.out.print(num * i);
        }
        System.out.println();
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 09:05:00