java-10x15乘法表(嵌套循环)

时间:2017-03-01 17:29:53

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

我需要使用for循环,if-else语句和while循环构造10x15乘法。这是它应该是什么样子:

what it's supposed to look like

我的代码:

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天了,而我还没有使用过一段时间。非常感谢任何帮助。

3 个答案:

答案 0 :(得分:2)

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

其次,您需要将调用移至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  

答案 1 :(得分:0)

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

答案 2 :(得分:0)

继承人是我的更新代码:

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