以相反的顺序打印阵列,printf对齐问题

时间:2015-03-15 06:45:11

标签: java arrays printf

我有一个数组,按列排序,每列10个。我试图反过来但排序已关闭,我不知道为什么。有人可以帮我看看吗?谢谢!

import java.util.Scanner;
import java.io.*;
import java.util.Arrays;

public class Pham_ArrayProcessing
{

    private static int[] fileArray;
    private static int arraySize;

// Main method

    public static void main(String[] args) throws IOException
    {
        inputData();
        printArray(fileArray);
        reverseArray(fileArray);
    }

// This method will ask the user for a file and read in the file location.
// If =/= exist, gives error msg and terminates program.
// Otherwise, creates array and store integers from file in the array.

    public static int[] inputData() throws IOException
    {
        System.out.println("");
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Please enter input file: ");
        String userFilename = keyboard.nextLine();

        File userFile = new File(userFilename);
        Scanner inputFile = new Scanner(userFile);

        arraySize = inputFile.nextInt();
        fileArray = new int[arraySize];

        if(userFile.exists())
        {   
            for (int i = 0; i < arraySize; i++)
            {
                fileArray[i] = inputFile.nextInt();
            }
        }
        else
        {
            System.out.println(userFilename + " was not found.");
            System.exit(0);     
        }
        inputFile.close();
        return fileArray;
    }

// This method prints out the passed in array to the screen with 10 integers per line.
// Uses the printf method to align the data in columns.

    public static void printArray(int[] array)
    {
        System.out.println("");
        System.out.println("Printed Array: ");
        for (int j = 0; j < arraySize; j++)
        {
            System.out.printf("%5s", fileArray[j]);
            if (j % 10 == 9)
                System.out.println("");
        }
        System.out.println("");
    }   

// This method prints out the passed in array in reverse order to the screen.
// Uses the printf method to align the data in columns.

    public static void reverseArray(int[] array)
    {
        System.out.println("");
        System.out.println("Reversed Array: ");
        for (int j = arraySize - 1; j >= 0; j--)
        {
            System.out.printf("%5s", fileArray[j]);
            if (j % 10 == 9)
                System.out.println("");
        }
        System.out.println("");
    }

我的输出是:

c:\Users\name\Desktop>java Pham_ArrayProcessing

Please enter input file: inputFile5.dat

Printed Array:
  -5  -77   43   64  -82   -9    2  -28  -36  -32
  86  -49   10  -14  -42  -36  -86  -22   -6  -12
  45   28   48   60    0

Reversed Array:
   0   60   48   28   45  -12
  -6  -22  -86  -36  -42  -14   10  -49   86  -32
 -36  -28    2   -9  -82   64   43  -77   -5

我想每行10个整数。

3 个答案:

答案 0 :(得分:0)

reverse打印功能中 之前:

  if (j % 10 == 9)
        System.out.println("");

更新:

  if ((arraySize - j) % 10 == 9)
        System.out.println("");

答案 1 :(得分:0)

 if (j % 10 == 9)
                System.out.println("");

此行会导致您的问题。例如,如果你的数组有19个托盘,它将打印最后一个条目,然后19 % 10 == 9将结果为真。这将导致它在之后插入换行符。

您的if语句假设它正在计算到目前为止打印的数字。在反向数组的情况下,j不是到目前为止打印的元素数,而是数组的索引。编写固定版本的最明确方法是引入一个新变量,它是与此类似的打印值的数量。

       int n = 0;
       for (int j = arraySize - 1; j >= 0; j--)
        {
            System.out.printf("%5s", fileArray[j]);
            if (n++ % 10 == 9)
                System.out.println("");
        }

虽然你可以用j做一些技巧来达到相同的效果。

答案 2 :(得分:0)

这应该工作,也做一些重构,不需要有一个数组大小int,你可以计算实际大小,实际上,如果你通过param数组,你应该打印数组[j]阵列。

  public static void reverseArray(int[] array)
        {
    System.out.println("");

    System.out.println("Reversed Array: ");
    for (int j = array.size() - 1; j >= 0; j--)
    {
        System.out.printf("%5s", array[j]);
        if ((fileArray.size()-1 - j) % 10 == 9){
        System.out.println("");
    }
    }

但也许更好的方法:

printArray(Collections.reverse(fileArray)) 

并编辑printArray方法以打印作为param传递的数组:

System.out.printf("%5s", array[j]);