使用行间距格式化乘法表

时间:2023-02-01 09:20:04

I'm trying to make a multiplication table which is as big as the user specifies. The problem I'm having is with the format. The output right now prints all the numbers to one line, whereas I want it all on multiple lines on a nice neat table. With it formatted as it is, I can't tell where the \n would go to do so, or if there is another way.

我正在尝试制作一个与用户指定一样大的乘法表。我遇到的问题是格式。输出现在将所有数字打印到一行,而我想在一个漂亮整洁的表上多行。随着它的格式化,我无法分辨\ n将会这样做,或者是否有另一种方式。

here's my code:

这是我的代码:

import java.util.*;

public class question2 {

  public static void main(String []  args) {

    Scanner keyb = new Scanner(System.in);

    int i = 0;

    while (i<=0 || i>=11) {
       System.out.print("please enter an integer between 1 and 10: ");
       i = keyb.nextInt();
    }

    for (int x = 1; x <= i; x++) {
       System.out.printf("%4d",x);
       for (int y = 1; y <= i; y++){
          System.out.printf("%4d",x*y);
       }
    }
  }
}

EDIT: The output for integer 5, prints like this:

编辑:整数5的输出,打印如下:

1 1 2 3 4 5 2 2 4 6 8 10 3 3 6 9 12 15 4 4 8 12 16 20 5 5 10 15 20 25

1 1 2 3 4 5 2 2 4 6 8 10 3 3 6 9 12 15 4 4 8 12 16 20 5 5 10 15 20 25

2 个解决方案

#1


0  

You should add a newline after the second for loop, and since you print x don't print x * 1. So,

您应该在第二个for循环后添加换行符,并且因为您打印x不打印x * 1.所以,

for (int x = 1; x <= i; x++) {
    System.out.printf("%4d", x);
    for (int y = 2; y <= i; y++) {
        System.out.printf("%4d", x * y);
    }
    System.out.println();
}

or you could eliminate the first print like,

或者你可以消除第一个印刷品,如,

for (int x = 1; x <= i; x++) {
    for (int y = 1; y <= i; y++) {
        System.out.printf("%-4d", x * y); // and simply for variety, left-aligned
    }
    System.out.println();
}

or if you are using Java 8+, you might use IntStream like

或者如果您使用的是Java 8+,则可以使用IntStream

IntStream.rangeClosed(1, i).forEachOrdered(x -> {
    IntStream.rangeClosed(1, i).forEachOrdered(y -> System.out.printf(
                "%-4d", x * y));
    System.out.println();
});

#2


2  

After the second for loop, add a new line \n:

在第二个for循环之后,添加一个新行\ n:

for (int x = 1; x <= i; x++) {
    System.out.printf("%4d",x);
    for (int y = 1; y <= i; y++){
        System.out.printf("%4d",x*y);
    }
    System.out.println();
}

#1


0  

You should add a newline after the second for loop, and since you print x don't print x * 1. So,

您应该在第二个for循环后添加换行符,并且因为您打印x不打印x * 1.所以,

for (int x = 1; x <= i; x++) {
    System.out.printf("%4d", x);
    for (int y = 2; y <= i; y++) {
        System.out.printf("%4d", x * y);
    }
    System.out.println();
}

or you could eliminate the first print like,

或者你可以消除第一个印刷品,如,

for (int x = 1; x <= i; x++) {
    for (int y = 1; y <= i; y++) {
        System.out.printf("%-4d", x * y); // and simply for variety, left-aligned
    }
    System.out.println();
}

or if you are using Java 8+, you might use IntStream like

或者如果您使用的是Java 8+,则可以使用IntStream

IntStream.rangeClosed(1, i).forEachOrdered(x -> {
    IntStream.rangeClosed(1, i).forEachOrdered(y -> System.out.printf(
                "%-4d", x * y));
    System.out.println();
});

#2


2  

After the second for loop, add a new line \n:

在第二个for循环之后,添加一个新行\ n:

for (int x = 1; x <= i; x++) {
    System.out.printf("%4d",x);
    for (int y = 1; y <= i; y++){
        System.out.printf("%4d",x*y);
    }
    System.out.println();
}