【问题标题】:Left justification in Java [duplicate]Java中的左对齐[重复]
【发布时间】:2018-04-07 02:29:55
【问题描述】:

对于我的课程,我们必须编写一个程序来接受用户输入并打印出许多斐波那契数列。我设法做到了,但我的教授希望它的字段宽度为 15 并且向左对齐。出于某种原因,我不明白,它只是不会给我正确的输出。这是我所拥有的:

boolean valid = false;  
while (valid == false) {

    System.out.println("Please enter a valid number (between: 1 - 50):");
    Scanner scan = new Scanner(System.in);
    int n = scan.nextInt();

    if  (n < 1) {
       System.out.println("The number entered " + n + " is not valid");
    }
    else if (n > 50) {
       System.out.println("The number entered " + n + " is not valid");
    } 
    else if (n >= 1 || n <= 50) {
       valid = true;
       //System.out.printf("%15s", "1 ");
       int a = 0;
       int b = 1;
       for (int i = 1; i <= n; i++) {
           int nextNumber = a + b;
           if (i%5 != 0) {
               System.out.printf("%15s", nextNumber + " ");
           }
           else {
               System.out.printf("%15s", nextNumber + " " + "\n");
           }
           a = b;
           b = nextNumber;

输出是右对齐的,但是当我将其设为“%-15s”时,这些值并没有全部正确排列,它仍然是右对齐的。另外,注释掉的那一行是因为我无法得到打印出第一行的序列。

【问题讨论】:

  • 如果你问的只是文本对齐,那么这个问题中关于斐波那契数列的所有内容都是无关紧要的
  • 问题似乎不是指定问题的重复,OP知道-修饰符。

标签: java


【解决方案1】:

我认为您不需要 printf 中的空格——这就是 %-15 提供的。另外,顺便说一句,您绝对不需要布尔值或 while 循环。这似乎对我有用:

public class Test { 

    public static void main(String[] args) {
        new Test().fib();
    }

    void fib() {
        System.out.println("Please enter a number between 1 and 50:");
        Scanner scan = new Scanner(System.in);
        final int n = scan.nextInt();

        if (n < 1 || n > 50) {
            System.out.println("The number entered is not valid");
            System.exit(0);
        }

        int a = 0;
        int b = 1;
        for (int i = 1; i <= n; i++) {
            int next = a + b;
            System.out.printf("%-15d", next);
            if (i % 5 == 0) System.out.println();
            a = b;
            b = next;
        }
    }

}

例如:

Please enter a number between 1 and 50:
45
1              2              3              5              8              
13             21             34             55             89             
144            233            377            610            987            
1597           2584           4181           6765           10946          
17711          28657          46368          75025          121393         
196418         317811         514229         832040         1346269        
2178309        3524578        5702887        9227465        14930352       
24157817       39088169       63245986       102334155      165580141      
267914296      433494437      701408733      1134903170     1836311903 

【讨论】:

    【解决方案2】:

    %-15s 格式的代码中的主要问题是这一行:

    System.out.printf("%-15s", nextNumber + " " + "\n");
    

    你得到了字符串,例如,"123 \n" 并用空格填充它,但是这些空格是在新行的\n 之后添加的,这会造成所有混乱(一种可能的修复方法这是在格式字符串中包含\n"%-15s\n")。看似右对齐的文本实际上是左对齐的文本,行首有空格。

    我们需要解决这个问题。此外,我们可以摆脱转换为字符串、多余的空格、单独处理第一个数字:

    int a = 0;
    int b = 1;
    
    for (int i = 0; i <= n; i++) {
        System.out.printf("%-15d", b); 
        if (i % 5 == 4) System.out.println();
    
        int nextNumber = a + b;
        a = b;
        b = nextNumber; 
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-03
      • 1970-01-01
      • 2020-07-09
      • 1970-01-01
      • 2015-10-13
      相关资源
      最近更新 更多