打印在Java中如表一个二维数组 [英] Printing a 2d array in Java like a table

查看:155
本文介绍了打印在Java中如表一个二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打印inputed 2维数组如表
即如果由于某种原因,他们把所有秒...

  1 1 1 11 1 1 11 1 1 11 1 1 1

就像上​​面如此但在日食的Java控制台,没有华丽的按钮和图形用户界面的,但在控制台中,这里是我......

 进口java.util.Scanner中;
    公共类客户{
        公共静态无效的主要(字串[] args){
            扫描仪输入=新的扫描仪(System.in);            INT [] []表=新INT [4] [4];
            的for(int i = 0; I< table.length;我++){
                对于(INT J = 0; J< table.length; J ++){
                    的System.out.println(输入一个数字。);
                    INT X = input.nextInt();
                    表[I] [J] = X;
                    System.out.print(表[I] [J] +);
                }
                    的System.out.println();
        }
        的System.out.println(表);
    }
}

这是我所得到的,当我输入的一切和控制台终止:

 输入一个数字。11输入一个数字。11输入一个数字。11输入一个数字。11[我@ 3fa1732d


解决方案

您需要从输入数字分别打印出数组。所以,你可以做这样的事情:

 公共类PrintArray {
    公共静态无效的主要(字串[] args){
        扫描仪输入=新的扫描仪(System.in);        INT [] []表=新INT [4] [4];
        的for(int i = 0; I< table.length;我++){
            对于(INT J = 0; J< table.length; J ++){
                //的System.out.println(输入一个数字。);
                INT X = input.nextInt();
                表[I] [J] = X;
            }
            //System.out.println();
        }
        //的System.out.println(表);        的for(int i = 0; I< table.length;我++){
            对于(INT J = 0; J<表[I]。长度; J ++){
                System.out.print(表[I] [J] +);
            }
            的System.out.println();
        }
    }
}

I'd like to print an inputed 2-dimensional array like a table i.e if for some reason they put in all 1s...

1 1 1 1 

1 1 1 1 

1 1 1 1

1 1 1 1 

Just like so above but in the console on Java eclipse, no fancy buttons and GUI's but in the console, here is what I have....

    import java.util.Scanner;
    public class Client {
        public static void main(String[] args){
            Scanner input = new Scanner(System.in);

            int[][] table = new int[4][4];
            for (int i=0; i < table.length; i++) {
                for (int j=0; j < table.length; j++) {
                    System.out.println("Enter a number.");
                    int x = input.nextInt();
                    table[i][j] = x;
                    System.out.print(table[i][j] + " "); 
                } 
                    System.out.println();
        }
        System.out.println(table);
    }
}

And this is what I get when I input everything and the console terminates:

Enter a number.

1

1 Enter a number.

1

1 Enter a number.

1

1 Enter a number.

1

1 

[[I@3fa1732d

解决方案

You need to print out the array separately from entering the number. So you can do something like this:

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

        int[][] table = new int[4][4];
        for (int i = 0; i < table.length; i++) {
            for (int j = 0; j < table.length; j++) {
                // System.out.println("Enter a number.");
                int x = input.nextInt();
                table[i][j] = x;
            }
            //System.out.println();
        }
        // System.out.println(table);

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

这篇关于打印在Java中如表一个二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆