Java数组练习题(敲完这些题直接下一章节)【03】

一、每天练十道题目找找手感

1、判断语句练习题:https://blog.csdn.net/m0_55586329/article/details/122441587

2、循环语句练习题:https://blog.csdn.net/m0_55586329/article/details/122451061

3、判断语句理论知识:https://blog.csdn.net/m0_55586329/article/details/116331989

4、数组理论实践知识(强烈推荐观看):https://blog.csdn.net/m0_55586329/article/details/116840485

Java数组练习题

1、已知数组{1, 3, 6, 7, 2, 5, 9, 0},输出数组的最大数字和最小数字。

public class Demo01 {
   
    public static void main(String[] args) {
   
        int[] arrays = {
   1, 3, 6, 7, 2, 5, 9, 0};
        // 方法一:遍历数组的方式
        // 先定义最大值为第一位先,然后进行比较
        int max = arrays[0];
        int min = arrays[0];
        for (int i = 0; i < arrays.length; i++) {
   
            if (max < arrays[i]) {
   
                max = arrays[i];
            }
            if (min > arrays[i]) {
   
                min = arrays[i];
            }
        }
        System.out.println("最大值Max=" + max + " 最小值Min=" + min);
        // 方法二:使用封装好的方法
        System.out.println("Max=" + Arrays.stream(arrays).max().getAsInt() + "  Min=" + Arrays.stream(arrays).min().getAsInt());
    }
}

2、已知数组{1, 3, 6, 7, 2, 5, 9, 0},如何将数组进行从小到大的排序?最后打印出来。

public class Demo02 {
   
    public static void main(String[] args) {
   
        int[] arrays = {
   1, 3, 6, 7, 2, 5, 9, 0};
        // 方法一:选择排序
        for (int i = 0; i < arrays.length; i++) {
   
            for (int j = i + 1; j < arrays.length; j++) {
   
                if (arrays[i] &g
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值