【发布时间】:2026-01-12 03:30:02
【问题描述】:
我必须根据用户的输入打印一个乘法表。 到目前为止,这是我的代码:
import java.util.Scanner;
public class problem7
{
public static void main (String args[])
{
int n1,n2,n3;
int i1, i2;
char again;
Scanner input=new Scanner(System.in);
System.out.println("Please enter the starting number:");
n1=input.nextInt();
System.out.println("\nPlease enter the ending number:");
n2=input.nextInt();
System.out.print("\n ");
for (i1=1; i1<=10; i1++){
System.out.print(" "+i1);
}
for (n1=n1; n1<=n2; n1++ ){
System.out.print("\n "+n1);
for (i2=1; i2<=10; i2++){
n3=n1*i2;
System.out.print(" "+n3);
}
}
}
}
这是输出:
Please enter the starting number:
2
Please enter the ending number:
4
1 2 3 4 5 6 7 8 9 10
2 2 4 6 8 10 12 14 16 18 20
3 3 6 9 12 15 18 21 24 27 30
4 4 8 12 16 20 24 28 32 36 40
一旦有一个具有 2 个有效数字的整数,间距就会变得混乱。我该如何解决?谢谢!
【问题讨论】:
-
使用
System.out.printf。查看here 了解更多示例