Java练习题 (2024.7.23)

        Exercise1

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

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

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

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);

    }
}

 

 

 

IntelliJ IDEA 是一款强大的集成开发环境 (IDE),常用于Java和其他多种语言开发。版本 2024.1.3是一个假设的版本,但你可以按照以下步骤创建一个Java项目: 1. **打开IntelliJ IDEA**: 打开安装的 IntelliJ IDEA,确保你已经登录了账户(如果你有)。 2. **新建项目**: - **菜单选择**: 在欢迎界面或顶部菜单栏中,点击 "File"(文件) > "New"(新建)> "Project"(项目)。 3. **选择项目类型**: 在打开的 "Create New Project" 窗口中,选择 "Java" 作为项目类型,并选择 "Java" 作为模板。 4. **设置项目结构**: 为项目命名,可以选择创建空项目或者从现有源码导入。你可以选择 "From Sources",然后指定Java源代码所在的目录,或者选择 "Gradle" 或 "Maven" 如果你的项目依赖于构建工具。 5. **配置SDK**: 确认或选择你的Java版本,如果你已经安装了多个JDK,可以从下拉列表中选择。 6. **添加依赖和库**: 如果你的项目需要第三方库,可以在 "Libraries" 部分添加JAR文件或通过 Gradle或Maven管理依赖。 7. **配置模块**: 对于大型项目,可能需要创建模块。选择 "New Module" 并配置每个模块的属性。 8. **完成设置**: 点击 "Finish" 来创建项目。IDEA会为你创建项目的目录结构,设置编译器选项,并初始化必要的文件。 9. **启动项目**: 创建完成后,可以通过 "Run" 或 "Debug" 按钮启动新创建的Java应用。 **相关问题**: 1. IntelliJ IDEA支持哪些版本的Java? 2. 如何在IntelliJ IDEA中查看和编辑项目结构? 3. 创建Java项目时,如何管理依赖和插件? 4. IntelliJ IDEA有没有提供自动代码补全和错误检查的功能?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值