常见Java编程题2

4.用Java写出倒三角形。

public class InvertedTriangle {
    public static void main(String[] args) {
        int rows = 5; // 倒三角形的行数

        printInvertedTriangle(rows);
    }

    public static void printInvertedTriangle(int rows) {
        for (int i = rows; i >= 1; i--) {
            for (int j = 1; j <= rows - i; j++) {
                System.out.print(" "); // 打印空格来对齐
            }

            for (int j = 1; j <= i * 2 - 1; j++) {
                System.out.print("*"); // 打印星号
            }

            System.out.println(); // 换行
        }
    }
}

在上述代码中,我们定义了一个 printInvertedTriangle 方法来打印倒三角形。我们使用两层循环来控制行和列。外层循环 i 控制行数,从 rows 递减到 1,内层循环 j 控制每一行中的列数。

在内层循环中,首先打印一定数量的空格来对齐,空格的数量为 rows - i,然后打印一定数量的星号,星号的数量为 i * 2 - 1,即每行递减。

通过运行上述代码,你将得到输出结果为如下的倒三角形:

*****
 ****
  ***
   **
    *

你可以根据需要调整 rows 的值,以打印不同行数的倒三角形。

如果你更希望使用更简单的方法打印倒三角形,你可以尝试以下的代码示例:

public class InvertedTriangle {
    public static void main(String[] args) {
        int rows = 5; // 倒三角形的行数

        printInvertedTriangle(rows);
    }

    public static void printInvertedTriangle(int rows) {
        for (int i = rows; i >= 1; i--) {
            for (int j = 1; j <= rows - i; j++) {
                System.out.print(" "); // 打印空格来对齐
            }

            for (int j = 1; j <= i; j++) {
                System.out.print("*"); // 打印星号
            }

            System.out.println(); // 换行
        }
    }
}

在这个简化版的代码中,我们使用了与之前相同的逻辑。外层循环 i 控制行数,从 rows 递减到 1。内层循环 j 控制每一行中的列数。

在内层循环中,我们仍然打印一定数量的空格来对齐,空格的数量为 rows - i。然后,我们直接打印 i 个星号。

通过运行这段代码,你将得到与之前相同的输出结果为如下的倒三角形:

*****
 ****
  ***
   **
    *

这种简化的方式避免了计算每行星号的数量,而是直接使用行数 i 作为星号的数量,使代码更加简洁。

5.用Java写出正倒三角。

public class Triangle {
    public static void main(String[] args) {
        int rows = 5; // 三角形的行数

        printTriangle(rows);
        printInvertedTriangle(rows);
    }

    // 打印正三角形
    public static void printTriangle(int rows) {
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= rows - i; j++) {
                System.out.print(" "); // 打印空格来对齐
            }

            for (int j = 1; j <= i * 2 - 1; j++) {
                System.out.print("*"); // 打印星号
            }

            System.out.println(); // 换行
        }
    }

    // 打印倒三角形
    public static void printInvertedTriangle(int rows) {
        for (int i = rows; i >= 1; i--) {
            for (int j = 1; j <= rows - i; j++) {
                System.out.print(" "); // 打印空格来对齐
            }

            for (int j = 1; j <= i * 2 - 1; j++) {
                System.out.print("*"); // 打印星号
            }

            System.out.println(); // 换行
        }
    }
}

在上述代码中,我们定义了两个方法:printTriangle 用于打印正三角形,printInvertedTriangle 用于打印倒三角形。

printTriangle 方法中的逻辑与之前的代码相同,使用两层循环控制行和列,打印相应数量的空格和星号。

printInvertedTriangle 方法也与之前的代码类似,使用两层循环控制行和列,打印相应数量的空格和星号。

通过运行上述代码,你将得到输出结果为如下的正倒三角形:

    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

你可以根据需要调整 rows 的值,以打印不同行数的正倒三角形。

6.正三角形。

public class Triangle {
    public static void main(String[] args) {
        int rows = 5; // 三角形的行数

        printTriangle(rows);
    }

    public static void printTriangle(int rows) {
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= rows - i; j++) {
                System.out.print(" "); // 打印空格来对齐
            }

            for (int j = 1; j <= 2 * i - 1; j++) {
                System.out.print("*"); // 打印星号
            }

            System.out.println(); // 换行
        }
    }
}

在上述代码中,我们定义了一个 printTriangle 方法来打印正三角形。我们使用两层循环来控制行和列。外层循环 i 控制行数,从 1 增加到 rows。内层循环 j 控制每一行中的列数。

在内层循环中,首先打印一定数量的空格来对齐,空格的数量为 rows - i。然后打印一定数量的星号,星号的数量为 2 * i - 1,即每行递增。

通过运行上述代码,你将得到输出结果为如下的正三角形:

    *
   ***
  *****
 *******
*********

你可以根据需要调整 rows 的值,以打印不同行数的正三角形。

7.输入年月日,判断今年是闰年还是平年。

import java.util.Scanner;

public class LeapYearChecker {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入年份:");
        int year = scanner.nextInt();

        if (isLeapYear(year)) {
            System.out.println(year + "年是闰年。");
        } else {
            System.out.println(year + "年是平年。");
        }

        scanner.close();
    }

    public static boolean isLeapYear(int year) {
        // 闰年的判断规则:能被4整除但不能被100整除,或者能被400整除
        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    }
}

在上述代码中,我们定义了一个 isLeapYear 方法用来判断给定的年份是否为闰年。根据闰年的规则,能被4整除但不能被100整除的年份是闰年,或者能被400整除的年份也是闰年。

main 方法中,我们通过 Scanner 类获取用户输入的年份。然后,我们调用 isLeapYear 方法来判断该年份是否为闰年,并输出相应的结果。

除了使用闰年的判断规则之外,我们还可以使用Java提供的java.time.Year类来判断给定年份是否为闰年。下面是使用Year类的示例代码:

import java.time.Year;
import java.util.Scanner;

public class LeapYearChecker {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入年份:");
        int year = scanner.nextInt();

        if (isLeapYear(year)) {
            System.out.println(year + "年是闰年。");
        } else {
            System.out.println(year + "年是平年。");
        }

        scanner.close();
    }

    public static boolean isLeapYear(int year) {
        return Year.of(year).isLeap(); // 调用Year类的isLeap()方法判断是否为闰年
    }
}

在这段代码中,我们利用Year.of(year)方法创建了一个Year对象,并通过isLeap()方法来判断该年份是否为闰年。

使用Year类进行闰年判断的好处是它提供了更多的功能和灵活性,比如可以获取闰年的总数、获取下

8.输入x,y,z,并由小到大排列。 一个闰年等。

import java.util.Scanner;

public class SortNumbers {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入x的值:");
        int x = scanner.nextInt();

        System.out.print("请输入y的值:");
        int y = scanner.nextInt();

        System.out.print("请输入z的值:");
        int z = scanner.nextInt();

        System.out.println("输入的数字按从小到大排序为:");

        if (x <= y && x <= z) {
            System.out.print(x + " ");

            if (y <= z) {
                System.out.print(y + " " + z);
            } else {
                System.out.print(z + " " + y);
            }
        } else if (y <= x && y <= z) {
            System.out.print(y + " ");

            if (x <= z) {
                System.out.print(x + " " + z);
            } else {
                System.out.print(z + " " + x);
            }
        } else {
            System.out.print(z + " ");

            if (x <= y) {
                System.out.print(x + " " + y);
            } else {
                System.out.print(y + " " + x);
            }
        }

        scanner.close();
    }
}

在上述代码中,我们使用Scanner类获取用户输入的x、y和z的值。然后,我们通过条件语句和比较运算符来比较这三个数,并按照从小到大的顺序进行排列输出。

我们也可以使用更简单的方法来实现输入x、y和z,并将它们按照从小到大的顺序排列。以下是另一种示例代码:

import java.util.Arrays;
import java.util.Scanner;

public class SortNumbers {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入x的值:");
        int x = scanner.nextInt();

        System.out.print("请输入y的值:");
        int y = scanner.nextInt();

        System.out.print("请输入z的值:");
        int z = scanner.nextInt();

        int[] numbers = {x, y, z};
        Arrays.sort(numbers);

        System.out.println("输入的数字按从小到大排序为:" + Arrays.toString(numbers));

        scanner.close();
    }
}

在上述代码中,我们使用Scanner类获取用户输入的x、y和z的值,并将它们存储在一个整数数组numbers中。然后,我们使用Arrays.sort()方法对数组进行排序,该方法会自动按照从小到大的顺序进行排列。

最后,我们使用Arrays.toString()方法将排序后的数组以字符串形式输出。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值