Java练习题 (2024.7.23)

Exercise1

java 复制代码
package Exercise20240723;
import java.util.Scanner;
import java.util.ArrayList;
public class Exercise1 {
    public static void main(String[] args) {
        /* 需求:
        键盘录入一些1~10日之间的整数,并添加到集合中。直到集合中所有数据和超过200为止。 */
        Scanner sc = new Scanner(System.in);
        ArrayList<Integer> arrayList = new ArrayList<>();
        int result;
        while (true) {
            result = getSum(arrayList);
            if (result > 200) {
                System.out.println("已经超过两百,现在集合的和是" + result);
                break;
            }
            System.out.println("请输入1个整数");
            int number = sc.nextInt();
            if (rightNumber(number)) {
                arrayList.add(number);
            } else {
                System.out.println("整数不和规范,请重新输入");
            }
        }
    }

    public static boolean rightNumber(int number) {
        if (number >= 1 && number <= 100 ) {
            return true;
        }
        return false;
    }

    public static int getSum(ArrayList<Integer> arrayList) {
        int sum = 0;
        for (Integer i : arrayList) {
            sum += i;
        }
        return sum;
    }
}

Exercise2

java 复制代码
package Exercise20240723;
import java.util.Scanner;
public class Exercise2 {
    public static void main(String[] args) {
//        需求:
//        自己实现parseInt方法的效果,将字符串形式的数据转成整数。
//        要求:字符串中只能是数字不能有其他字符最少一位,最多10位0不能开头
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串");
        String str = sc.nextLine();
        if (!str.matches("[1-9]\\d{0,9}")) {
            System.out.println("不合规的字符串");
            System.exit(1);
        }
        int number = 0;
        for (int i = 0; i < str.length(); i++) {
            number = number * 10 + (str.charAt(i) - '0');
        }
        System.out.println(number);
        System.out.println(number + 5);

    }
}

Exercise3

java 复制代码
package Exercise20240723;
import java.util.Scanner;
public class Exercise3 {
    public static void main(String[] args) {
//        需求:
//        定义一个方法自己实现toBinaryString方法的效果,将一个十进制整数转成字符串表示的二进制
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个整数");
        int number = sc.nextInt();
        String str = "";
        while (number > 0) {
            str = number % 2 + str;
            number /= 2;
        }
        System.out.println(str);
    }
}

Exercise4

java 复制代码
package Exercise20240723;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Scanner;
import java.util.Date;
public class Exercise4 {
    public static void main(String[] args) throws ParseException {
//        需求:
//        请使用代码实现计算你活了多少天,用JDK7和JDK8两种方式完成
        Scanner sc = new Scanner(System.in);
        // JDK7;
        System.out.println("请输入你的生日");
        String birthday = sc.nextLine();
        SimpleDateFormat birthdayFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date birthdayDate = birthdayFormat.parse(birthday);
        Long birthdayTime = birthdayDate.getTime();
        System.out.println("已经活了" + ((System.currentTimeMillis() - birthdayTime) / 1000 / 3600 / 24) + "天");

        // JDK8
        LocalDate birthday8 = LocalDate.of(2004, 10, 14);
        LocalDate nowTime = LocalDate.now();
        long days = ChronoUnit.DAYS.between(birthday8, nowTime);
        System.out.println(days);

    }
}
相关推荐
love530love几秒前
【PyCharm必会基础】正确移除解释器及虚拟环境(以 Poetry 为例 )
开发语言·ide·windows·笔记·python·pycharm
凌辰揽月2 分钟前
Web后端基础(基础知识)
java·开发语言·前端·数据库·学习·算法
海奥华26 分钟前
go中的接口返回设计思想
开发语言·后端·golang
lifallen8 分钟前
深入浅出 Arrays.sort(DualPivotQuicksort):如何结合快排、归并、堆排序和插入排序
java·开发语言·数据结构·算法·排序算法
运维开发王义杰8 分钟前
Python: 告别 ModuleNotFoundError, 解决 pipx 环境下 sshuttle 缺少 pydivert 依赖的终极指南
开发语言·python
k要开心9 分钟前
从C到C++语法过度1
开发语言·c++
长安不见10 分钟前
背景知识: 理解LimitLatch背后的AQS
java
小吕学编程12 分钟前
策略模式实战:Spring中动态选择商品处理策略的实现
java·开发语言·设计模式
weixin_4383354018 分钟前
Spring Boot实现接口时间戳鉴权
java·spring boot·后端
q5673152333 分钟前
IBM官网新闻爬虫代码示例
开发语言·分布式·爬虫