简体   繁体   English

Java乘法表

[英]Java Multiplication Table

I'm making a java program that shows the multiplication table that looks like this: 我正在制作一个显示如下乘法表的Java程序:

1 1个

But I can only get the results from 1 to 5th column. 但是我只能从第1列到第5列获得结果。 How do I make the rest appear below ? 我如何使其余显示在下面?

The program must only contain one for() nested loop. 该程序只能包含一个for()嵌套循环。

This is my code so far: 到目前为止,这是我的代码:

import java.util.Scanner;
public class Table{
    public static void main(String[] args){

        Scanner s = new Scanner(System.in);
        System.out.print("Enter a number: ");

        int inputi = s.nextInt();

        for(int i = 1 ;i<=10;i++) {

            for(int j=1;j<=inputi && j <= 5;j++) {
                System.out.print(j + " x " + i + " = "  +(i*j) + "\t");
            }

            System.out.println();

            if(i >= 5)
            for(int j = 6; j <= inputi && j <= 10; j++){
                System.out.print(j + " x " + i + " = "  +(i*j) + "\t");
            }
        }

        System.out.println();
        System.out.println();
    }
}

Can anyone help ? 有人可以帮忙吗?

Thanks. 谢谢。

Edit: Sample input added. 编辑:添加了示例输入。

Inputi = 7 输入i = 7

Expected output: 预期产量:

预期产量

Actual output: 实际输出:

实际产量

This algorithm with only 2 layers of for loops will give you the correct output. 这种只有2层for循环的算法将为您提供正确的输出。

import java.util.Scanner;

public class Table {
    public static void main(String[] args){

        Scanner s = new Scanner(System.in);
        System.out.print("Enter a number: ");

        int inputi = s.nextInt();

        for(int i = 0; i < Math.ceil(inputi / 5.0) * 10; i++) {
            if ( i > 0 && i % 10 == 0 ) System.out.println();

            int t = inputi - 5 * (i / 10);
            for(int j = 0; j < (t > 5 ? 5 : t); j++) {
                int a = 5 * (i / 10) + j + 1;
                int b = i % 10 + 1;
                System.out.print(a + " x " + b + " = "  + (a * b) + "\t");
            }
            System.out.println();
        }
    }
}

Here is the sample output for input=11 : 这是input=11的示例输出:

1 x 1 = 1   2 x 1 = 2   3 x 1 = 3   4 x 1 = 4   5 x 1 = 5   
1 x 2 = 2   2 x 2 = 4   3 x 2 = 6   4 x 2 = 8   5 x 2 = 10  
1 x 3 = 3   2 x 3 = 6   3 x 3 = 9   4 x 3 = 12  5 x 3 = 15  
1 x 4 = 4   2 x 4 = 8   3 x 4 = 12  4 x 4 = 16  5 x 4 = 20  
1 x 5 = 5   2 x 5 = 10  3 x 5 = 15  4 x 5 = 20  5 x 5 = 25  
1 x 6 = 6   2 x 6 = 12  3 x 6 = 18  4 x 6 = 24  5 x 6 = 30  
1 x 7 = 7   2 x 7 = 14  3 x 7 = 21  4 x 7 = 28  5 x 7 = 35  
1 x 8 = 8   2 x 8 = 16  3 x 8 = 24  4 x 8 = 32  5 x 8 = 40  
1 x 9 = 9   2 x 9 = 18  3 x 9 = 27  4 x 9 = 36  5 x 9 = 45  
1 x 10 = 10 2 x 10 = 20 3 x 10 = 30 4 x 10 = 40 5 x 10 = 50 

6 x 1 = 6   7 x 1 = 7   8 x 1 = 8   9 x 1 = 9   10 x 1 = 10 
6 x 2 = 12  7 x 2 = 14  8 x 2 = 16  9 x 2 = 18  10 x 2 = 20 
6 x 3 = 18  7 x 3 = 21  8 x 3 = 24  9 x 3 = 27  10 x 3 = 30 
6 x 4 = 24  7 x 4 = 28  8 x 4 = 32  9 x 4 = 36  10 x 4 = 40 
6 x 5 = 30  7 x 5 = 35  8 x 5 = 40  9 x 5 = 45  10 x 5 = 50 
6 x 6 = 36  7 x 6 = 42  8 x 6 = 48  9 x 6 = 54  10 x 6 = 60 
6 x 7 = 42  7 x 7 = 49  8 x 7 = 56  9 x 7 = 63  10 x 7 = 70 
6 x 8 = 48  7 x 8 = 56  8 x 8 = 64  9 x 8 = 72  10 x 8 = 80 
6 x 9 = 54  7 x 9 = 63  8 x 9 = 72  9 x 9 = 81  10 x 9 = 90 
6 x 10 = 60 7 x 10 = 70 8 x 10 = 80 9 x 10 = 90 10 x 10 = 100   

11 x 1 = 11 
11 x 2 = 22 
11 x 3 = 33 
11 x 4 = 44 
11 x 5 = 55 
11 x 6 = 66 
11 x 7 = 77 
11 x 8 = 88 
11 x 9 = 99 
11 x 10 = 110   

I would suggest to 我建议

  1. Create separate variable with number of required columns 创建具有所需列数的单独变量
  2. Calculate how many rows will be printed (see iterations ) 计算将要打印多少行(请参阅iterations
  3. Print rows one by one 一张一张打印

Please, see the code snippet below: 请查看下面的代码片段:

int inputi = 12;
int columns = 5;
int iterations = inputi / columns + (inputi % columns > 0 ? 1 : 0);
for (int iter = 0; iter < iterations; iter++) {
    for (int i = 1; i <= 10; i++) {
        for (int j = iter * columns + 1; j <= Math.min(inputi, (iter + 1) * columns); j++) {
            System.out.print(j + " x " + i + " = "  +(i * j) + "\t\t");
        }
        System.out.println();
    }
    System.out.println();
}

Note, in case inputi is huge, you may have to fill outputs with additional spaces, to avoid layout issues, when you have statements like 1 x 1 = 1 and 1 x 10000000 = 1000000 请注意,如果inputi很大,则当您有类似1 x 1 = 11 x 10000000 = 1000000语句时,可能必须用额外的空格填充输出,以避免布局问题

This will give you the desired output: 这将为您提供所需的输出:

import java.util.Scanner;

public class Table{
public static void main(String[] args){

    Scanner s = new Scanner(System.in);
    System.out.print("Enter a number: ");

    int inputi = s.nextInt();
    for(int i = 1 ;i<=10;i++) {

        for(int j=1;j<=inputi && j <= 5;j++) {
            System.out.print(j + " x " + i + " = "  +(i*j) + "\t");
        }

        System.out.println();
    }

    System.out.println();
    System.out.println();

    for(int i = 1 ;i<=10;i++) {

        for(int j=6;j<=inputi && j <= 10;j++) {
            System.out.print(j + " x " + i + " = "  +(i*j) + "\t");
        }

        System.out.println();
    }

  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM