程序员成长之路(Day 13)

目录

学习目标:

学习内容:

LintCode刷题:

·比较字符串

 ·创建ArrayList集合并添加数据

· 打印春夏秋冬的特点

 ·使用自定义异常

 ·根据分数判断是否及格

· 计算 n 以内包括 n 所有能被 3 整除的正整数的和

学习产出:


学习内容:

LintCode刷题:

·比较字符串

         首先创建一个全部都是0的数组,长度到128,再将A字符串遍历,所有字符所在位置都将数字++,然后遍历B字符串,所有字符所在位置都将数字--,当这个数组中有<0的部分就返回false,其余返回ture即可

public class Solution {
    public boolean compareStrings(String A, String B) {
        int[] countsByChar = new int[128];
        for (char c : A.toCharArray()) {
            countsByChar[c]++;
        }
        for (char c : B.toCharArray()) {
            countsByChar[c]--;
            if (countsByChar[c] < 0) return false;
        }
        return true;
    }
}

 ·创建ArrayList集合并添加数据

         创建String类型的ArrayList然后添加数据并返回

import java.util.ArrayList;
public class Solution {

    public ArrayList<String> createList(String str1, String str2) {
        ArrayList<String> arrayList=new ArrayList<String>();
        arrayList.add(str1);
        arrayList.add(str2);
        return arrayList;
    }
}

· 打印春夏秋冬的特点

         使用枚举,创建以及方法使用

public enum Season {
    SPRING("spring","spring returns and everything comes back to life"),
    SUMMER("Summmer", "the summer heat is sweetest with you"),
    AUTUMN("Autumn", "autumn winds and floodwaters rise"),
    WINTER("Winter", "the north wind is howling and the snow is drifting");
    private String name;
    private String feature;

    Season(String name, String feature) {
        this.name = name;
        this.feature = feature;
    }

    public String getName() {
        return name;
    }

    public String getFeature() {
        return feature;
    }

    @Override
    public String toString() {
        return name + ": " + feature + ".";
    }
}
public class Main {
    Season spring;
    public Main(Season spring) {
        this.spring=spring;
    }

    public static void main(String[] args) {
        Main
                SPRING=new Main(Season.SPRING),
                SUMMER=new Main(Season.SUMMER),
                AUTUMN=new Main(Season.AUTUMN),
                WINTER=new Main(Season.WINTER);
        SPRING.toString();
        SUMMER.toString();
        AUTUMN.toString();
        WINTER.toString();
    }
}

 ·使用自定义异常

         自定以的异常继承父异常,使用方法也是调用父异常的方法,使用时抛出即可

class MyException extends Exception{
    public MyException(String message){
        super(message);
    }
}
public class Solution {

    public void validate(int num) throws MyException {
        if (num>=0&&num<=100){
            System.out.println(num);
        }else
            throw new MyException("The number you entered is not legal");
    }
}
public class Main {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int num = Integer.parseInt(args[0]);

        try {
            solution.validate(num);

            if ((num < 0) || (num > 100)) {
                throw new RuntimeException("You need to throw a custom exception");
            }
        } catch (MyException e) {
            System.out.println(e.getMessage());
        }
    }
}

 ·根据分数判断是否及格

        根据输入的成绩判断即可 

public class Solution {
    public String scoreCompare(int score) {
       if (score>=60){
           return "passed";
       }else
           return "failed";
    }
}

· 计算 n 以内包括 n 所有能被 3 整除的正整数的和

        定义了Int i=3则使用while循环即可

public class Solution {
    public int add(int number) {
        int i = 3;
        int sum = 0;
        if (number<3)return 0;
        while (i<=number){
            if (i%3==0)
                sum+=i;
            i++;
        }
        return sum;
    }
}

 学习时间:

2021-8-21 16:00-18:00、19:00-22:30


学习产出:

刷题*6

学习笔记*1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值