• 第七章练习题


    第七章练习题

    第一题

    题目:题目描述
    代码:

    public class HomeWork01{
       
    	public static void main(String[] args){
       
    		double[] arr = {
       34.5, 56.8, 129.3, 22.4, 103.5};
    		A01 a1 = new A01();
    		System.out.println("数组内最大值为" + a1.max(arr));
    	}
    }
    class A01{
       
    	public double max(double[] arr){
       
    		double maxNum = arr[0];
    		for(int i = 1; i < arr.length; i++){
       
    			if(maxNum < arr[i]){
       
    				maxNum = arr[i];
    			}
    		}
    		return maxNum;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    进一步优化:假如数组内元素的个数为0

    public class HomeWork01{
       
    	public static void main(String[] args){
       
    		double[] arr = {
       };
    		A01 a1 = new A01();
    		System.out.println("数组内最大值为" + a1.max(arr));
    	}
    }
    class A01{
       
    	public double max(double[] arr){
       
    		double maxNum = arr[0];
    		for(int i = 1; i < arr.length; i++){
       
    			if(maxNum < arr[i]){
       
    				maxNum = arr[i];
    			}
    		}
    		return maxNum;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    编译运行结果
    在这里插入图片描述
    完善代码

    public class HomeWork01{
       
    	public static void main(String[] args){
       
    		double[] arr = {
       };
    		A01 a1 = new A01();
    		Double res = a1.max(arr);
    		if(res != null){
       
    			System.out.println("数组内最大值为" + a1.max(arr));
    		}else{
       
    			System.out.println("数组输入有误...");
    		}
    		
    	}
    }
    class A01{
       
    	public Double max(double[] arr){
       
    		//保证arr至少有一个元素(涉及到代码健壮性)
    		if(arr.length > 0){
       
    			double maxNum = arr[0];
    			for(int i = 1; i < arr.length; i++){
       
    				if(maxNum < arr[i]){
       
    					maxNum = arr[i];
    				}
    			}
    			return maxNum;
    		}else{
       
    			return null;
    		}
    		
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    分析:方法中的数据返回类型由double改成Double,当数组内为空时,返回一个null
    运行结果
    在这里插入图片描述
    再进一步完善代码
    假如double[] arr = null;

    public class HomeWork01{
       
    	public static void main(String[] args){
       
    		double[] arr = null;
    • 1
    • 2
    • 3
    • 4
  • 相关阅读:
    llama-factory SFT 系列教程 (四),lora sft 微调后,使用vllm加速推理
    省 市 县 三级联动
    Python办公自动化【Word】
    艾美捷重组蛋白酶K,无动物源/AF性质和稳定性
    【JavaSE语法】运算符
    docker安装配置nginx
    SpringBoot SpringBoot 开发实用篇 4 数据层解决方案 4.9 MongoDB 下载与安装
    基于SSM的大学餐厅菜品推荐和点评系统设计与实现
    C++继承
    教育的外呼是如何搭建的?
  • 原文地址:https://blog.csdn.net/wjkqq0921/article/details/127648023