Skip to content

Latest commit

 

History

History
34 lines (23 loc) · 1008 Bytes

File metadata and controls

34 lines (23 loc) · 1008 Bytes

Java 程序:以显示数字的因数

原文: https://www.programiz.com/java-programming/examples/factors-number

在此程序中,您将学习如何使用 Java 中的for循环显示给定数字的所有因数。

示例:正整数的因数

public class Factors {

    public static void main(String[] args) {

        int number = 60;

        System.out.print("Factors of " + number + " are: ");
        for(int i = 1; i <= number; ++i) {
            if (number % i == 0) {
                System.out.print(i + " ");
            }
        }
    }
}

运行该程序时,输出为:

Factors of 60 are: 1 2 3 4 5 6 10 12 15 20 30 60

在上述程序中,要查找其因素的编号存储在变量number中(60)。

重复执行for循环,直到i <= number为假。 在每次迭代中,检查number是否可被i精确整除(inumber的因数)和i的值增加 1。