JAVA基础之题目练习

JAVA基础之题目练习
摘要由CSDN通过智能技术生成

0x01

编写类A01,定义方法max,实现求某个double数组的最大值,并返回Homework01.java

public class Homework01 {
   
    public static void main(String[] args){
   
        double[] numbers = {
   534, 1,34, 90, 56.9, 23, 45, 103.981};
        A01 a01 = new A01();
        System.out.println("The max number is " + a01.max(numbers));;
    }
}


class A01{
   
    public double max(double[] number){
   
        double maxnum = number[0];
        int i = 0;
        for(; i < number.length; i++){
   
            maxnum = maxnum > number[i] ? maxnum : number[i];
        }

        return maxnum;
    }
}

The max number is 534.0

0x02

编写类A02,定义方法find,实现查找某字符串数组中的元素查找,并返回索引,如果找不到,返回-1。

public class Homework02 {
   
    public static void main(String[] args){
   
        String[] data = {
   "hello", "hi", "yes", "no"};
        String finddata = "yes";
        A02 a02 = new A02();
        int index = a02.find(data, finddata);
        if(index == -1){
   
            System.out.println("NoFind, the index is " + index);
        }else {
   
            System.out.println("Find, the index is " + index);
        }
    }
}

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


Find, the index is 2

0x03

编写类Book,定义方法updatePrice,实现更改某本书的价格,具体:如果价格>150,则更改为150,如果价格>100,更改为100,否则不变。

public class Homework03 {
   
    public static void main(String[] args){
   
        book bo = new book("xiyouji", 102.4);
        bo.updatePrice();
    }
}

class book{
   

    String name;
    double price;

    public book(String name, double price){
   
        this.name = name;
        this.price = price;
    }
    public void updatePrice(){
   
        if(price > 150)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值