hsp 循序渐进学java第七章的作业题

 第一题

public class Homework01{
	public static void main(String [] args){
	double[] array = {1.1,2.2,2,3,1,0,3.4,3,100,20,-100};
	//若array = null;怎么处理,在if条件句中加入array!=null
	//若数组为空,即array = {};怎么处理,用if条件句中加入 array.length > 0,
	//由于是在方法中要返回double类型,而else里无法处理,故用包装类。
	A01 a1 = new A01();
	Double n1 = a1.max(array);
		if(array != null && n1 != null){
			System.out.println(n1);
		}else{
			System.out.println("array输入有误");
		}
	}
}

class A01{
	public Double max(double[] array){
		if(array.length > 0){
			double max = array[0];
			for (int i = 1;i<array.length ;i++ ) {
				
				if(array[i]>max){
					max = array[i];
				}
			}
			return max;
		}else{
			//也需要返回一个double类型
			//解决办法:首先要知道对象是可以为空的,即Person p1 = null;(正确)
			//将public double的 double 改成Double(包装类),
			//并接受该类的对象,为空或者double类型的数字
			return null;
		}
	}
}

第二题

public class Homework02{
	public static void main(String [] args){
		String[] array = {"123","11","abc","nike","你好"};
		A02 a02 = new A02();
		int index = a02.find("ab",array);
		if(index != -1 ){
		System.out.println(index);
		}else{
			System.out.println("没有找到");
		}
	}
}


class A02{
	public int find(String str , String[] array){
		int j = -1;
		for (int i = 0;i < array.length ;i++ ) {
			
			
			if(array[i].equals(str)){
				return i;
			}
		}
		return j;
	}
}

第三题 

//用关键字this来做
public class Homework03{
	public static void main(String [] args){
		Book book = new Book("三味书屋", 222.0);
		book.updatePrice();	
	}
}

class Book{
	String name;
	double price;

	public Book(String name , double price){
 		this.name = name;
 		this.price = price;
	}
	public void updatePrice(){
		if(this.price > 0.0){
			if (this.price > 150.0) {
				 price = 150.0;
				System.out.println("书名" +this.name +"的新价格为" + this.price); 
			}else i
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值