如何在java中的列中打印出来

时间:2014-04-22 12:03:06

标签: java printing

这是我要打印的地方,我需要它在列中打印.aLeaderboard是一个带有自定义类的数组列表。它包含几个不同的整数

System.out.println("Position     Team                Games Played    Home Wins   Home Draws   Home Losses  Home Goals For   Home Goals Against   Away Wins   Away Draws  Away Losses  Away Goals For    Away Goals Against   Goal Difference    Total Points");
  for(int counter = 0;counter<teamName.size();counter++)
  {
  System.out.print((counter + 1) + "            " +  teamName.get(counter) + "     " + (aLeaderboard.get(counter)).getGamesPlayed() + "                  " + (aLeaderboard.get(counter)).getHomeWins() + "              " +  (aLeaderboard.get(counter)).getHomeDraws() + "           ");
  System.out.print((aLeaderboard.get(counter)).getHomeLosses() + "        " + (aLeaderboard.get(counter)).getAwayWins() + "      " + (aLeaderboard.get(counter)).getAwayWins() + "       " + (aLeaderboard.get(counter)).getAwayDraws() + "        ");
  System.out.print((aLeaderboard.get(counter)).getHomeGoalsFor() + "         " + (aLeaderboard.get(counter)).getHomeGoalsAgainst() + "          " + (aLeaderboard.get(counter)).getAwayLosses() + "        " + (aLeaderboard.get(counter)).getGamesPlayed() + "     ");
  System.out.print((aLeaderboard.get(counter)).getAwayGoalsFor() + "         " + (aLeaderboard.get(counter)).getAwayGoalsAgainst() + "          " + (aLeaderboard.get(counter)).getGoalsDifference() + "          " + (aLeaderboard.get(counter)).getTotalPoints());
  System.out.println();
  }

1 个答案:

答案 0 :(得分:2)

我会使用System.out.printf(...)并使用模板字符串来帮助确保所有列都排成一行。然后你可以在for循环中轻松打印出来。

例如:

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

public class Foo4 {
   public static void main(String[] args) {
      List<Bar4> bar4List = new ArrayList<>();
      bar4List.add(new Bar4("Donald", 3, "A", 22.42));
      bar4List.add(new Bar4("Duck", 100, "B", Math.PI));
      bar4List.add(new Bar4("Herman", 20, "C", Math.sqrt(20)));

      String titleTemplate = "%-10s %6s %6s %9s%n";
      String template = "%-10s %6d %6s %9s%n";

      System.out.printf(titleTemplate, "Name", "Value", "Grade", "Cost");
      for (Bar4 bar4 : bar4List) {
         System.out.printf(template, bar4.getName(), 
               bar4.getValue(), bar4.getGrade(), bar4.getCostString());
      }

   }
}

class Bar4 {
   private String name;
   private int value;
   private String grade;
   private double cost;
   private NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();

   public Bar4(String name, int value, String grade, double cost) {
      this.name = name;
      this.value = value;
      this.grade = grade;
      this.cost = cost;
   }

   public String getName() {
      return name;
   }

   public int getValue() {
      return value;
   }

   public String getGrade() {
      return grade;
   }

   public double getCost() {
      return cost;
   }

   public String getCostString() {
      return currencyFormat.format(cost);
   }

}

哪会回来:

Name        Value  Grade      Cost
Donald          3      A    $22.42
Duck          100      B     $3.14
Herman         20      C     $4.47

有关String格式说明符用户的更多详细信息(例如,上面的%6d和%6s),请查看Formatter API