【问题标题】:Java printf formatting to print items in a table or columnsJava printf 格式化以打印表或列中的项目
【发布时间】:2015-11-01 19:55:50
【问题描述】:

我想使用 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]

我有这个代码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 ] 

任何帮助将不胜感激。

【问题讨论】:

标签: java printf multiple-columns


【解决方案1】:

我会用这个:

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

【讨论】:

    【解决方案2】:

    您可以获得所有单词字符串的最大长度,并将该长度存储在变量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);
    

    【讨论】:

    • 您可能需要在打印前检查每列的最大值
    【解决方案3】:
    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
    

    【讨论】:

      【解决方案4】:

      输入:

      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]
      

      代码:

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

      输出:

      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]
      

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

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

      【讨论】:

        猜你喜欢
        • 2013-10-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多