【问题标题】:java-10x15 multiplication table (nested loops)java-10x15 乘法表(嵌套循环)
【发布时间】:2017-03-01 17:29:53
【问题描述】:

我需要使用 for 循环、if-else 语句和 while 循环构造一个 10x15 乘法。它应该是这样的:

我的代码:

import java.util.Scanner;

public class Question2 {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);      
        final int IMAX = 15;
        final int JMAX = 10;

        System.out.print("   |");
        for(int j = 1; j<=JMAX; j++) {
            System.out.print("      " + j);
            // This printed the top header
        }
        System.out.println();
        System.out.println("____________________________________________________________________________");

        for(int i = 1; i<=IMAX; i++) {
            System.out.print("  " + i + " |");

            for (int j = 1; j <=JMAX; j++) {
                if(j <= i) 
                    System.out.print( i*j + "    ");
                else
                    System.out.println();   
                }
            }

        }

    }

}

它正在输出:

我已经很接近搞清楚了,但我已经卡了 3 天了,我还没有使用过 while 循环。非常感谢任何帮助。

【问题讨论】:

  • 因此,您需要将其中一个 for 循环转换为 while 循环。尝试搜索“convert for to while loop”

标签: java for-loop while-loop nested-loops


【解决方案1】:

首先,您应该开始使用调试器,如果您不知道如何使用,那么现在是学习的好时机。它将极大地帮助您解决未来的语法和逻辑问题。

其次,您需要将对println() 的调用移动到外部for 循环,您应该只在j 等于i 之后向下移动一行。例如:

for(int i = 1; i <= IMAX; i++) {
    System.out.print("  " + i + " |");

    // 'j' should never exceed 'i', just make that the for loop condition,
    // no need for an if statement in the body of the for loop.
    for (int j = 1; j <= i; j++) {
        System.out.print( i * j + "    ");
    }
    // move call to 'println()' here
    System.out.println();
}

您也可以用 while 循环替换嵌套的 for 循环,如下所示:

for(int i = 1; i <= IMAX; i++) {
    System.out.print("  " + i + " |");

    int j = 1;
    while (j <= i) {
        System.out.print( i * j + "    ");
        j += 1;
    }
    // move call to 'println()' here
    System.out.println();
}

请注意,内部循环将始终至少运行一次,因此可以使用 do-while 循环:

for(int i = 1; i <= IMAX; i++) {
    System.out.print("  " + i + " |");

    int j = 1;
    do {
        System.out.print( i * j + "    ");
        j += 1;
    } while (j <= i);

    // move call to 'println()' here
    System.out.println();
}

完整的工作代码:

public static void main(String[] args) {
    //Scanner keyboard = new Scanner(System.in);      
    final int IMAX = 15;
    final int JMAX = 10;

    System.out.print("   |");
    for(int i = 1; i <= IMAX; i++) {
        System.out.printf("%5d", i);
        // This printed the top header
    }
    System.out.println("\n____________________________________________________________________________");

    for(int i = 1; i<=IMAX; i++) {
        System.out.printf("%2d |    ", i);

        int j = 1;
        while(j <= i) {
            System.out.printf("%-5d", i * j);
            j += 1;
        }
        System.out.println();
    }

}

输出:

   |    1    2    3    4    5    6    7    8    9   10   11   12   13   14   15
____________________________________________________________________________
 1 |    1    
 2 |    2    4    
 3 |    3    6    9    
 4 |    4    8    12   16   
 5 |    5    10   15   20   25   
 6 |    6    12   18   24   30   36   
 7 |    7    14   21   28   35   42   49   
 8 |    8    16   24   32   40   48   56   64   
 9 |    9    18   27   36   45   54   63   72   81   
10 |    10   20   30   40   50   60   70   80   90   100  
11 |    11   22   33   44   55   66   77   88   99   110  121  
12 |    12   24   36   48   60   72   84   96   108  120  132  144  
13 |    13   26   39   52   65   78   91   104  117  130  143  156  169  
14 |    14   28   42   56   70   84   98   112  126  140  154  168  182  196  
15 |    15   30   45   60   75   90   105  120  135  150  165  180  195  210  225  

【讨论】:

  • 非常感谢!!帮了我很多..只有一件事,桌子需要 10x15 ..所以你知道当 j=10 时如何水平停止桌子..再次感谢!
  • 基本上我正在尝试添加一个 j
  • @HousseinKharroubi 将 while 条件更改为 while (j &lt;= i &amp;&amp; j &lt;= JMAX)
【解决方案2】:

您应该将System.out.println(); 放在循环之后,而不是放在else 语句中。因为它仍在遍历第二个循环并打印那些空格

【讨论】:

    【解决方案3】:

    这是我更新的代码:

    import java.util.Scanner;
    

    公开课问题2 {

    public static void main(String[] args) 
    {
        Scanner keyboard = new Scanner(System.in);  
    
        final int IMAX = 15;
        final int JMAX = 10;
    
        System.out.print("    |");
        for(int j = 1; j<=JMAX; j++)
            {
            System.out.print(" " + j + "     ");
            // This printed the top header
            }
    
        System.out.println();
        System.out.println("__________________________________________________________________________________________________________");
    
    
            for(int i = 1; i <= IMAX; i++)
                {
                    if(i<=9)
                        System.out.print(" " + i + "  |");
                    else
                        System.out.print(" " + i + " |");
    
    
                    int j = 1;
                    while (j <= i) 
                    {
                        System.out.printf(" " + "%-6d", i * j);
                        j += 1;
                    }
                // move call to 'println()' here
                System.out.println();
                }
    
    
    
    
    
    }
    

    它输出this

    我要that

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-14
      • 2021-11-27
      • 1970-01-01
      • 1970-01-01
      • 2016-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多