java - 打印居中对齐的二维数组

标签 java arrays methods printing

嗨,我正在尝试打印一个居中对齐的二维数组,但如果我正确的话,数字会指向内存单元。我该如何让他们打印实际的数字,我尝试创建一个显示方法,但没有成功。到目前为止,这是我的代码。在弄清楚这一点后,我还将找到最小值、最大值和平均值。

import java.util.Scanner;

public class Print2DArray {

public static void main(String[] args) {

    Scanner print2d = new Scanner(System.in);
    System.out.println("Please enter the number of rows: ");
    int rows = print2d.nextInt();
    System.out.println("Please enter the number of columns: ");
    int columns = print2d.nextInt();

    int array[][] = new int[rows][columns];

    System.out.println("\nVictor  - 002017044\n");

    for (int x = 0; x < array.length; x++) {
        for (int y = 0; y < array[x].length; y++) {
            int value = (int) (Math.random() * 10000);
            value = (int) (Math.round((value * 100)) / 100.0);
            array[x][y] = value;
        }
    }

    for (int x = 0; x < array.length; x++) {
        for (int y = 0; y < array[x].length; y++) {
            printArray(array);
        }
        System.out.println();
    }


    int max = 0;
    int avg = 0;
    int min = 0;

    System.out.println("\nMaximum: " + max + "\nAverage: " + avg
            + "\nMinimum: " + min);
}

private static void printArray(int [][] array){
    int width = 6;
    int leftSP = (width - array.length)/2;
    int rightSP = width - array.length - leftSP;
    for (int i = 0; i < leftSP; i++) {
        System.out.print(" ");
    }
        System.out.print(array);
    for (int i =0; i < rightSP; i++) {
        System.out.print(" ");
    }
}
}

最佳答案

我不太确定在这种情况下居中对齐是什么意思。将某些内容居中需要您知道允许打印多少空间,然后将要显示的内容均匀分布到宽度/2 的两侧。例如,默认情况下,在 cmd.exe 中,长度限制为 80 个字符,但这可以更改。不过,我认为这个答案的核心在于: Can I find the console width with Java?

基本上,你无法将其居中。您所能期望的最好的结果就是将其左对齐(或尝试根据某些任意预先确定的宽度将其居中)。

根据您所写的内容以及我在 printArray 中看到的内容,您的另一个问题是您不知道如何打印数组索引处的值。在我解决这个问题之前,我必须解决你写的一些事情

but the numbers point to the memory cell if im correct

这实际上是不正确的。根据 java.lang.Object#toString 方法,这是 toString 方法的默认功能:

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())

http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#toString%28%29

您的打印方法可能应该如下所示:

private static void printArray(int [][] array){
    if(array == null || array.length < 1 || array[0].length < 1)
        throw new IllegalArgumentException("array must be non-null, and must have a size of at least \"new int[1][1]\"");
    for (int i = 0; i < array.length; i++) {
        for(int j = 0; j < array[0].length; j++)
            System.out.print("[" + array[i][j] + "]");
        System.out.println();
    }
}

编辑: 我看到您发表的评论,其中您指定了中心对齐的含义。基本上,您需要记录放入数组中的任何 int 的最大长度,如下所示:

//global max value
public static int maxLength = 0;
...
    //inside of Print2DArray.main(String [] args)
    for (int x = 0; x < array.length; x++) {
        for (int y = 0; y < array[x].length; y++) {
            value = (int) (Math.round((value * 100)) / 100.0);
            int numberOfDigits = String.valueOf(value).length();
            if(numberOfDigits > Print2DArray.maxLength)
                Print2DArray.maxLength = numberOfDigits;
            array[x][y] = value;
        }
    }

然后您将需要调整 printArray 函数的打印位置

System.out.print("[" + array[i][j] + "]");

System.out.printf("[%" + Print2DArray.maxLength + "d]", array[i][j]);

关于java - 打印居中对齐的二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24565525/

相关文章:

arrays - 如何在 MATLAB 中找到多个数组的最大值?

java - Java 是 "pass-by-reference"还是 "pass-by-value"?

file - flask 文件下载方法不允许错误

java - 套接字 getInetAddress 方法覆盖

java - 削减代码: "The method changeVote in the type VoteService is not applicable for the arguments (void)"

java - 是否可以将具有 2 个参数的函数作为 java 8/11 中另一个函数的参数传递?

java - 查找排序数组中整数出现的边界的问题 (Java)

php - 在特定条件下使用 PHP 进行排名

java - BigDecimal 除法具有大量小数位

java - 如何添加 UsernameTokenInterceptor cxf 拦截器