【问题标题】:Program that compute the multiplication table for all numbers less than or equal N in JAVA在JAVA中计算所有小于或等于N的数字的乘法表的程序
【发布时间】:2020-06-25 15:52:24
【问题描述】:

如何编写一个程序来计算所有小于或等于 N 的数字的乘法表。请注意,N 是从用户读取的整数。

程序会重复 这样做直到用户在 JAVA 中输入 -1

我不知道我是否应该为此使用嵌套循环或方法,但我编写了以下未完成的代码,这给了我一个无限循环

public static void main(String[] args) {
    int N ;
    System.out.println("Enter N: " );
    N = in.nextInt();

    while ( N != -1) {
        for(int i = 1; i <= N; ++i)
        {
            for (int c = 1; c <= 10; ++c)  
                System.out.println(N + "*" + c + " = " + (N*c));
        }
    }
}

我想要这样的输出:

Enter an integer to print it's multiplication table, -1 to
    exit
    2
    Multiplication table of 1
    1*1 = 1, 1*2 = 2, 1*3 = 3, 1*4 = 4, 1*5 = 5, 1*6 = 6, 1*7 =
    7, 1*8 = 8, 1*9 = 9, 1*10 = 10,
    Multiplication table of 2
    2*1 = 2, 2*2 = 4, 2*3 = 6, 2*4 = 8, 2*5 = 10, 2*6 = 12, 2*7
    = 14, 2*8 = 16, 2*9 = 18, 2*10 = 20, 
    Enter an integer to print it's multiplication table, -1 to
    exit  
    -1

【问题讨论】:

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


    【解决方案1】:

    Sunny Patel 的回答是正确的,但只是为了向您展示另一种方法:

    import java.util.Scanner;
    import java.util.stream.IntStream;
    
    public class Multiply {
        public static void main(String [] args) {
            try (Scanner in = new Scanner(System.in)) {
                int N;
                do {
                    System.out.println("Enter N: " );
                    N = in.nextInt();
                    IntStream.range(1, N+1)
                         .forEach(i -> IntStream.range(1, 11)
                                                .forEach(j -> System.out.println(String.format("%d*%d = %d", i, j, (i*j)))));
                } while ( N != -1);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    【讨论】:

    • 我喜欢在 Java 7 中添加 try with resources
    • 是的,太棒了。我只需要记住在编写代码时实际使用它。我已经做了将近 20 年的 Java,所以我有时会陷入旧习惯。
    【解决方案2】:

    您的代码在无限循环中运行,因为N 在外部for 循环内没有变化。

    您可以将提示放在循环中,并更改为do-while loop以保证至少执行1次;如果用户输入小于 1 的数字,则没有(因为外部 for-loop)。

    您还缺少对 Scanner 的引用来捕获输入。

    最后,你忘了在输出中使用i而不是N,否则内循环每次都会输出相同的值。

    import java.util.Scanner;                              // Import Scanner
    public static void main(String[] args) {
        int N;
        Scanner in = new Scanner(System.in);               // Missing Scanner
        do {                                               // Changed to do-while loop
            System.out.println("Enter N: " );
            N = in.nextInt();                              // Prompt user for N.
            for(int i = 1; i <= N; ++i)
            {
                for (int c = 1; c <= 10; ++c)
                    System.out.println(i + "*" + c + " = " + (i*c)); // Use i instead of N
            }
        } while ( N != -1);
    }
    

    【讨论】:

    • 我的错,我应该澄清我想要的输出......我将编辑我的问题
    • 嵌套循环没问题。创建一个函数只是将您的代码分解为可重复调用的可管理部分。
    • 我刚刚编辑了我的问题并添加了一个示例运行,我希望我能让你更清楚
    • 您应该将您的一些System.out.println 转换为System.out.print 以避免末尾的换行符。您甚至可以在每个for-loops 之后(外部)添加一个空的System.out.println()
    猜你喜欢
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    • 2019-02-27
    • 2019-09-25
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    相关资源
    最近更新 更多