Java 用行距格式化乘法表
我正在尝试制作一个乘法表,它和用户指定的一样大。我遇到的问题是格式。现在的输出将所有数字打印到一行,而我希望所有数字都打印在一张整洁的表格上的多行上。按照它的格式,我不知道Java 用行距格式化乘法表,java,Java,我正在尝试制作一个乘法表,它和用户指定的一样大。我遇到的问题是格式。现在的输出将所有数字打印到一行,而我希望所有数字都打印在一张整洁的表格上的多行上。按照它的格式,我不知道\n将在哪里执行此操作,或者是否有其他方法 这是我的密码: import java.util.*; public class question2 { public static void main(String [] args) { Scanner keyb = new Scanner(System.in)
\n
将在哪里执行此操作,或者是否有其他方法
这是我的密码:
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);
}
}
}
}
import java.util.*;
公开课问题2{
公共静态void main(字符串[]args){
扫描仪keyb=新扫描仪(System.in);
int i=0;
而(i=11){
System.out.print(“请输入一个介于1和10之间的整数:”);
i=keyb.nextInt();
}
对于(int x=1;x在第二个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();
}
for(int x=1;x您应该在第二个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();
}
整数5的输出是这样打印的:1 1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 5 5 10 15 20 25你能解释一下i
的用途吗,因为它每次都会覆盖旧值。我想你是在寻找ArrayList
的数组,是吗ht?System.out.println();
,或System.out.printf(“%n”);
看起来棒极了!谢谢!
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();
}
IntStream.rangeClosed(1, i).forEachOrdered(x -> {
IntStream.rangeClosed(1, i).forEachOrdered(y -> System.out.printf(
"%-4d", x * y));
System.out.println();
});