我需要显示一个包含数组价格的商品列表,并希望调整价格。我几乎让它工作但需要改进。下面是代码和输出。任何想法如何使所有价格一致?到目前为止有些工作,但有些工作没有。提前谢谢。
//for loop
System.out.printf("%d. %s \t\t $%.2f\n",
i + 1, BOOK_TYPE[i], COST[i]);
输出:
1. Newspaper $1.00
2. Paper Back $7.50
3. Hardcover book $10.00
4. Electronic book $2.00
5. Magazine $3.00
答案 0 :(得分:19)
您可以尝试以下示例。在宽度之前使用' - '以确保左缩进。默认情况下,它们会缩进;这可能不适合你的目的。
System.out.printf("%2d. %-20s $%.2f%n", i + 1, BOOK_TYPE[i], COST[i]);
格式字符串语法:http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax
格式化数字打印输出:https://docs.oracle.com/javase/tutorial/java/data/numberformat.html
PS:这可以作为对DwB答案的评论,但我仍然无权发表评论并回答它。
答案 1 :(得分:5)
printf和类似printf的方法的格式规范采用可选的宽度参数。
System.out.printf( "%10d. %25s $%25.2f\n",
i + 1, BOOK_TYPE[i], COST[i] );
将宽度调整为所需的值。
答案 2 :(得分:3)
一个让人想到的简单解决方案是拥有String
个空格块:
String indent = " "; // 20 spaces.
打印出字符串时,计算实际缩进并将其添加到结尾:
String output = "Newspaper";
output += indent.substring(0, indent.length - output.length);
这将调整字符串的空格数,并将它们全部放在同一列中。
答案 3 :(得分:1)
这是一个可以设置bookType列宽度的潜在解决方案(即bookTypes值的格式) 基于最长的bookTypes值。
public class Test {
public static void main(String[] args) {
String[] bookTypes = { "Newspaper", "Paper Back", "Hardcover book", "Electronic book", "Magazine" };
double[] costs = { 1.0, 7.5, 10.0, 2.0, 3.0 };
// Find length of longest bookTypes value.
int maxLengthItem = 0;
boolean firstValue = true;
for (String bookType : bookTypes) {
maxLengthItem = (firstValue) ? bookType.length() : Math.max(maxLengthItem, bookType.length());
firstValue = false;
}
// Display rows of data
for (int i = 0; i < bookTypes.length; i++) {
// Use %6.2 instead of %.2 so that decimals line up, assuming max
// book cost of $999.99. Change 6 to a different number if max cost
// is different
String format = "%d. %-" + Integer.toString(maxLengthItem) + "s \t\t $%9.2f\n";
System.out.printf(format, i + 1, bookTypes[i], costs[i]);
}
}
}
答案 4 :(得分:0)
您可以参考此博客在控制台上打印格式化的彩色文本
https://javaforqa.wordpress.com/java-print-coloured-table-on-console/
public class ColourConsoleDemo {
/**
*
* @param args
*
* "\033[0m BLACK" will colour the whole line
*
* "\033[37m WHITE\033[0m" will colour only WHITE.
* For colour while Opening --> "\033[37m" and closing --> "\033[0m"
*
*
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.println("\033[0m BLACK");
System.out.println("\033[31m RED");
System.out.println("\033[32m GREEN");
System.out.println("\033[33m YELLOW");
System.out.println("\033[34m BLUE");
System.out.println("\033[35m MAGENTA");
System.out.println("\033[36m CYAN");
System.out.println("\033[37m WHITE\033[0m");
//printing the results
String leftAlignFormat = "| %-20s | %-7d | %-7d | %-7d |%n";
System.out.format("|---------Test Cases with Steps Summary -------------|%n");
System.out.format("+----------------------+---------+---------+---------+%n");
System.out.format("| Test Cases |Passed |Failed |Skipped |%n");
System.out.format("+----------------------+---------+---------+---------+%n");
String formattedMessage = "TEST_01".trim();
leftAlignFormat = "| %-20s | %-7d | %-7d | %-7d |%n";
System.out.print("\033[31m"); // Open print red
System.out.printf(leftAlignFormat, formattedMessage, 2, 1, 0);
System.out.print("\033[0m"); // Close print red
System.out.format("+----------------------+---------+---------+---------+%n");
}