如何在 Java 中仅使用嵌套的 for 循环和 System.out.println 来显示乘法表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13094085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can a multiplication table be displayed using only nested for loops and System.out.println in Java?
提问by vijay
public static void main (String [] args)
{
int q, r, s, t, u, v, w, x, y, z;
for (q=1; q<=10; q++)
{
System.out.print("\t" + q);
}
System.out.println();
for (r=2; r<=20; r += 2)
{
System.out.print("\t" + r);
}
System.out.println();
for (s=3; s<=30; s += 3)
{
System.out.print("\t" + s);
}
System.out.println();
for (t=4; t<=40; t += 4)
{
System.out.print("\t" + t);
}
System.out.println();
for (u=5; u<=50; u += 5)
{
System.out.print("\t" + u);
}
System.out.println();
for (v=6; v<=60; v += 6)
{
System.out.print("\t" + v);
}
System.out.println();
for (w=7; w<=70; w += 7)
{
System.out.print("\t" + w);
}
System.out.println();
for (x=8; x<=80; x += 8)
{
System.out.print("\t" + x);
}
System.out.println();
for (y=9; y<=90; y += 9)
{
System.out.print("\t" + y);
}
System.out.println();
for (z=10; z<=100; z += 10)
{
System.out.print("\t" + z);
}
}
Despite how ridiculous this program looks, it displays a multiplication table in the desired format. Being a noob (as you can see) and trying to learn these nested loops has been very confusing, especially when the tutorial says that this multiplication table, in the same exact format, can be written using just nested for loops and System.out.println. The tutorial is not the least bit helpful and gives a rather simple use of nested for loops and right now I cannot see how it's applicable in simplifying this program....but it says it can be done so it can.
尽管这个程序看起来多么荒谬,但它以所需的格式显示了一个乘法表。作为一个菜鸟(如您所见)并试图学习这些嵌套循环非常令人困惑,尤其是当教程说这个乘法表以完全相同的格式编写时,可以仅使用嵌套的 for 循环和 System.out 来编写。打印。该教程一点帮助都没有,并且提供了嵌套 for 循环的相当简单的使用,现在我看不出它如何适用于简化这个程序......但它说它可以做到,所以它可以。
回答by dan
This is something that you will learn in any beginners book:
这是您将在任何初学者书中学到的东西:
for (int i=1;i<=10;i++){
for (int j=1;j<=10;j++)
System.out.print("\t"+i*j);
System.out.println();
}
回答by vijay
Here is my take on this with pretty printing (individual cells right/left justified):
这是我对漂亮打印的看法(单个单元格右/左对齐):
private static void printMultiplicationTable(int m, int n) {
int[][] arr = new int[m][n];
int[] maxes = new int[n];
for (int i = 1; i <= m; i++) {
for (int j = i, k = 1; j <= n * i; j += i) {
arr[i-1][k-1] = j;
if ((maxes[k-1]+"").length() < (j+"").length()) {
maxes[k-1] = (j+"").length();
}
k++;
}
}
for(int i=0; i<arr.length; i++) {
for(int j=0; j<arr[i].length; j++)
System.out.print(adjust(arr[i][j], maxes[j], true) + " ");
System.out.println();
}
}
private static String adjust(int n, int tot, boolean rightJustify) {
StringBuffer buff = new StringBuffer();
for(int i=1; i<=tot - (n + "").length(); i++) {
buff.append(" ");
}
if (rightJustify) {
buff.append(n);
return buff.toString();
} else {
return n + buff.toString();
}
}
回答by sunleo
This works as you expected using array
这可以按预期使用数组
public static void main(String[] args) {
int[][] array = new int[11][11];
for (int i=1; i<array.length; i++) {
for (int j=1; j<array[i].length; j++) {
array[i][j] = i*j;
System.out.print(" " + array[i][j]);
}
System.out.println("");
}
}
回答by Rohit Jain
You mean something like this: -
你的意思是这样的: -
for (int i = 1; i <= 10; i++) {
for (int j = i; j <= 10 * i; j += i) {
System.out.print(j + " ");
}
System.out.println();
}