gpt4 book ai didi

java - 如何在java中按列打印出来

转载 作者:行者123 更新时间:2023-11-30 03:55:50 25 4
gpt4 key购买 nike

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

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();
}

最佳答案

我将使用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

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

关于java - 如何在java中按列打印出来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23218961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com