📜  Java程序的输出| 17套(1)

📅  最后修改于: 2023-12-03 14:43:04.382000             🧑  作者: Mango

Java程序的输出

本题目共提供17套Java程序的输出,旨在帮助程序员掌握Java中输出相关的知识点。以下为各套程序的介绍及代码。

程序1

介绍:输出Hello World

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}
程序2

介绍:输出10以内的奇数

public class Main {
    public static void main(String[] args) {
        for (int i = 1; i <= 10; i += 2) {
            System.out.print(i + " ");
        }
    }
}
程序3

介绍:输出1到100之间的偶数

public class Main {
    public static void main(String[] args) {
        for (int i = 2; i <= 100; i += 2) {
            System.out.print(i + " ");
        }
    }
}
程序4

介绍:输出九九乘法表

public class Main {
    public static void main(String[] args) {
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j + "*" + i + "=" + (i * j) + " ");
            }
            System.out.println();
        }
    }
}
程序5

介绍:输出1到10之间的数字的平方

public class Main {
    public static void main(String[] args) {
        for (int i = 1; i <= 10; i++) {
            System.out.print(i * i + " ");
        }
    }
}
程序6

介绍:输出1到10之间的数字的阶乘

public class Main {
    public static void main(String[] args) {
        for (int i = 1; i <= 10; i++) {
            int result = 1;
            for (int j = 1; j <= i; j++) {
                result *= j;
            }
            System.out.print(result + " ");
        }
    }
}
程序7

介绍:输出1到100之间的质数

public class Main {
    public static void main(String[] args) {
        for (int i = 2; i <= 100; i++) {
            boolean isPrime = true;
            for (int j = 2; j < i; j++) {
                if (i % j == 0) {
                    isPrime = false;
                    break;
                }
            }
            if (isPrime) {
                System.out.print(i + " ");
            }
        }
    }
}
程序8

介绍:输出Fibonacci数列

public class Main {
    public static void main(String[] args) {
        int n = 10;
        int[] fibonacci = new int[n];
        fibonacci[0] = 0;
        fibonacci[1] = 1;
        for (int i = 2; i < n; i++) {
            fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2];
        }
        for (int i = 0; i < n; i++) {
            System.out.print(fibonacci[i] + " ");
        }
    }
}
程序9

介绍:输出100以内的完数

public class Main {
    public static void main(String[] args) {
        for (int i = 1; i <= 100; i++) {
            int sum = 0;
            for (int j = 1; j < i; j++) {
                if (i % j == 0) {
                    sum += j;
                }
            }
            if (sum == i) {
                System.out.print(i + " ");
            }
        }
    }
}
程序10

介绍:求1到100之间的所有偶数的和

public class Main {
    public static void main(String[] args) {
        int sum = 0;
        for (int i = 2; i <= 100; i += 2) {
            sum += i;
        }
        System.out.println("1到100之间的所有偶数的和为:" + sum);
    }
}
程序11

介绍:求1到100之间的能被7整除的数的和

public class Main {
    public static void main(String[] args) {
        int sum = 0;
        for (int i = 1; i <= 100; i++) {
            if (i % 7 == 0) {
                sum += i;
            }
        }
        System.out.println("1到100之间的能被7整除的数的和为:" + sum);
    }
}
程序12

介绍:求1到100之间的所有素数的和

public class Main {
    public static void main(String[] args) {
        int sum = 0;
        for (int i = 2; i <= 100; i++) {
            boolean isPrime = true;
            for (int j = 2; j < i; j++) {
                if (i % j == 0) {
                    isPrime = false;
                    break;
                }
            }
            if (isPrime) {
                sum += i;
            }
        }
        System.out.println("1到100之间的所有素数的和为:" + sum);
    }
}
程序13

介绍:求1到100之间的所有奇数的积

public class Main {
    public static void main(String[] args) {
        int product = 1;
        for (int i = 1; i <= 100; i += 2) {
            product *= i;
        }
        System.out.println("1到100之间的所有奇数的积为:" + product);
    }
}
程序14

介绍:求1到100之间的所有偶数的积

public class Main {
    public static void main(String[] args) {
        int product = 1;
        for (int i = 2; i <= 100; i += 2) {
            product *= i;
        }
        System.out.println("1到100之间的所有偶数的积为:" + product);
    }
}
程序15

介绍:求1到100之间的所有数字的和

public class Main {
    public static void main(String[] args) {
        int sum = 0;
        for (int i = 1; i <= 100; i++) {
            sum += i;
        }
        System.out.println("1到100之间的所有数字的和为:" + sum);
    }
}
程序16

介绍:输出1到1000之间的所有水仙花数

public class Main {
    public static void main(String[] args) {
        for (int i = 100; i <= 999; i++) {
            int a = i / 100;
            int b = (i - a * 100) / 10;
            int c = i - a * 100 - b * 10;
            if (i == a * a * a + b * b * b + c * c * c) {
                System.out.print(i + " ");
            }
        }
    }
}
程序17

介绍:输出1到100之间的所有回文数

public class Main {
    public static void main(String[] args) {
        for (int i = 1; i <= 100; i++) {
            int temp = i;
            int reverse = 0;
            while (temp != 0) {
                reverse = reverse * 10 + temp % 10;
                temp /= 10;
            }
            if (i == reverse) {
                System.out.print(i + " ");
            }
        }
    }
}