Java printf 格式化以打印表或列中的项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33466526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:42:52  来源:igfitidea点击:

Java printf formatting to print items in a table or columns

javaprintfmultiple-columns

提问by Vayelin

I am wanting to print things in a neat columns using printf.

我想使用 printf 在整齐的列中打印内容。

happening       0.083333    [4]
hi              0.083333    [0]
if              0.083333    [8]
important       0.083333    [7]
is              0.250000    [3, 5, 10]
it              0.166667    [6, 9]
tagged          0.083333    [11]
there           0.083333    [1]
what            0.083333    [2]

I have this code System.out.printf("%s %.6f %s \n", word, web.getFrequency(word), loc);, but it prints out this:

我有这个代码System.out.printf("%s %.6f %s \n", word, web.getFrequency(word), loc);,但它打印出这个:

happening  0.083333  [ 4 ] 
hi  0.083333  [ 0 ] 
if  0.083333  [ 8 ] 
important  0.083333  [ 7 ] 
is  0.250000  [ 3 5 10 ] 
it  0.166667  [ 6 9 ] 
tagged  0.083333  [ 11 ] 
there  0.083333  [ 1 ] 
what  0.083333  [ 2 ] 

Any help would be appreciated.

任何帮助,将不胜感激。

回答by dmolony

I would use this:

我会用这个:

System.out.printf ("%-30s %1.7f %s%n", word, etc, etc2);

回答by lakerka

You can get the max length of all the word strings, and store that length in a variable, max. Use a for loop to get that max, and then create a Stringthat has the format with concatenating the normal printf format with the max variable to be used as the width. Additionally \tputs in a tab character.

您可以获得所有单词字符串的最大长度,并将该长度存储在一个变量中max。使用 for 循环来获取它max,然后创建一个String具有将普通 printf 格式与用作宽度的 max 变量连接起来的格式。另外\t放入一个制表符。

int max = 0;
for (int ii = 0; ii < numberOfStrings; ii++)
{
   max = (word.length() > max) ? word.length() : max;
}
String format = "%" + max + "s\t%.6f\t%s \n";
System.out.printf(format, word, web.getFrequency(word), loc);

回答by Garen Goh

System.out.format("%-8s%-8s%-8s\n","a","b","pow(b,a)");
System.out.format("%-8d%-8d%-8d\n",1,2,1);
System.out.format("%-8d%-8d%-8d\n",2,3,8);
System.out.format("%-8d%-8d%-8d\n",3,4,81);
System.out.format("%-8d%-8d%-8d\n",4,5,1024);
System.out.format("%-8d%-8d%-8d\n",5,6,15625);

//the negative integer kinda sets it back to the number of space needed

回答by Shreya Sharma

input:

输入:

happening       0.083333    [4]
hi              0.083333    [0]
if              0.083333    [8]
important       0.083333    [7]
is              0.250000    [3, 5, 10]
it              0.166667    [6, 9]
tagged          0.083333    [11]
there           0.083333    [1]
what            0.083333    [2]

code:

代码:

import java.util.Scanner;
public class MyClass { 
    private static final Scanner sc = new Scanner(System.in);
       public static void main(String args[]) {
            for(int i = 0 ; i < 9; i++){
                String str = sc.next();
                float f = sc.nextFloat();
                String str2 = sc.nextLine();
                System.out.printf("%-10s %f %s\n", str, f, str2);
             }
             sc.close();
      }   
  }

output:

输出:

happening       0.083333    [4]
hi              0.083333    [0]
if              0.083333    [8]
important       0.083333    [7]
is              0.250000    [3, 5, 10]
it              0.166667    [6, 9]
tagged          0.083333    [11]
there           0.083333    [1]
what            0.083333    [2]

use - to make the string left aligned, use field width to align them properly. In your case printf statement will be

使用 - 使字符串左对齐,使用字段宽度正确对齐它们。在你的情况下 printf 语句将是

System.out.printf("%-10s %.6f %s\n", word, web.getFrequency(word), loc);