Java代码练习题

很多刚入门小白,初学者、新生初次接触编程,不知道如何提升自己的编程能力,不懂得如何快速入门编程。记住各种各样的代码。其实走上大佬的途径只有一个!!疯狂码代码!!!



1.使用冒泡排序法,并且通过自定义方法实现格式化输出

代码如下(示例):

public class ArrayDemo {
    public static void main(String[] args) {
        int[] arr={24,69,80,57,13,99,1,-8};
        System.out.println("排序前"+ arrayToString(arr));
        //冒泡排序
        for(int j =0;j<arr.length-1;j++) {
            for (int i = 0; i < arr.length - 1-j; i++) {
                if(arr[i] >arr[i+1]){
                    int temp = arr[i];
                    arr[i] = arr[i+1];
                    arr[i+1] =temp;
                }
            }
        }
        System.out.println("排序后的数组"+arrayToString(arr));
    }

    public static String arrayToString(int [] arr){
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (int i =0;i <arr.length;i++){
            if(i == arr.length-1){
                sb.append(arr[i]);
            }else{
                sb.append(arr[i]).append(",");
            }
        }
        sb.append("]");
        String s = sb.toString();
        return s;
    }
}

涉及包:ArrayList

2.认识Calendar,创建Calendar方法格式化显示当前系统时间

代码如下(示例):

public class CalendarDemo {
    public static void main(String[] args) {
        //获取对象
        Calendar c = Calendar.getInstance(); //多态形式定义对象

        //public int get (int field)
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH)+1;
        int date = c.get(Calendar.DATE);
        System.out.println(year+"年"+month+"月"+date+"日");

    }
}

涉及包:calendar:为某一时刻和一组日历字段之间的转换提供了一些方法,并为操作日历字段提供了一些方法

3.创建Calendar对象,实现获取任意一年二月有多少天。

代码如下(示例):

import java.util.Calendar;
import java.util.Scanner;

/*
    需求:
        获取任意一年的二月有多少天
    思路:
        1.键盘录入任意的年份
        2.设置日历对象的年、月、日
            年:来自于键盘录入
            月:设置为3月,月份是从0开始,所以设置的值是2
            日:设置为1日
        3.3月1日往前推一天,就是2月的最后一天
        4.获取这一天输出即可
 */
public class CalendarTest {
    public static void main(String[] args) {
        //键盘录入任意的年份
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入年份:");
        int year =sc.nextInt();

        //设置日历对象的年、月、日
        Calendar c = Calendar.getInstance();
        c.set(year,2,1);

        //3月1日往前推一天,就是2月的最后一天
        c.add(Calendar.DATE,-1);

        //获取这一天输出即可
        int date = c.get(Calendar.DATE);
        System.out.println("这一年2月的天数为:"+date);
    }
}

涉及包:calendar

4.通过多态的方式创建Collention集合对象,添加元素。

代码如下(示例):

import java.util.ArrayList;
import java.util.Collection;

/*
    创建Collention集合的对象
        多态的方式
        ArrayList()
 */
public class CollectionDemo01 {
    public static void main(String[] args) {
        //创阿金Collection集合的对象
        Collection<String> c = new ArrayList<String>();
        //添加元素,boolean add(E e)
        c.add("hello");
        c.add("hello");
        c.add("hello");
        System.out.println(c);
    }
}

涉及包:ArrayList,Collection

5.创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合

代码如下(示例):

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/*


    思路:
        1.定义学生类
        2.创建Collection集合
        3.创建学生对象
        4.把学生添加到集合
        5.遍历集合(迭代器方式)
 */
public class CollectionDemo {
    public static void main(String[] args) {
//        2.创建Collection集合
        Collection <Student> c = new ArrayList<Student>() ;

//        3.创建学生对象
         Student s1 = new Student("段亚乐",18);
         Student s2 = new Student("文格尔",18);
         Student s3 = new Student("尼斯列兵",18);

        //       4.把学生添加到集合
        c.add(s1);
        c.add(s2);
        c.add(s3 );

        //5.遍历集合(迭代器方式)
        Iterator<Student> it = c.iterator();
        while (it.hasNext()){
            Student s = it.next();
            System.out.println(s.getName()+","+s.getAge());
        }

    }

}

学生类:

public class Student {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

6.创建collection集合对象,通过集合的iterator()方法输出。

代码如下(示例):

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class IteratorDemo {
    public static void main(String[] args) {
        //创建集合对象
        Collection<String> c = new ArrayList<String>();

        //添加元素
        c.add("hello");
        c.add("world");
        c.add("java");

        //Iterator<E> iterator():返回此集合中元素的迭代器,通过集合的iterator()方法得到
        Iterator<String> it = c.iterator();

        while(it.hasNext()){
            String  s = it.next();
            System.out.println(s);
        }
    }
}

涉及包:ArrayList,Collection,Iterator

7.创建ArrayList集合,通过比较器就行比较排序。

代码如下(示例):

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class CollectionsDemo02 {
    public static void main(String[] args) {

        ArrayList<Student> array = new ArrayList<>();

        Student s1 = new Student("java", 18);
        Student s2 = new Student("asd", 20);
        Student s3 = new Student("zxc", 13);
        Student s4 = new Student("ert", 15);

        array.add(s1);
        array.add(s2);
        array.add(s3);
        array.add(s4);

        Collections.sort(array, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                //按照年龄从小到大排序,姓名字母顺序排序
                int num = s1.getAge() -s2.getAge();
                int num2 = num==0?s1.getName().compareTo(s2.getName()):num;
                return num2;
            }
        });
        for(Student s : array){
            System.out.println(s.getName()+","+s.getAge());
        }

    }
}

 学生类:

public class Student {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

8.通过递归算法计算1~20的和。

代码如下(示例):

public class DiGuiDemo {
    public static void main(String[] args) {
        int[] arr = new int[20];

        arr[0] =1;
        arr[1]=1;

        for(int i =2;i<arr.length;i++){
            arr[i] = arr[i-1]+arr[i-2];
        }
        System.out.println(arr[19]);

    }

    public static int f(int n){
        if(n==1 || n==2){
            return 1;
        }else{
            return f(n-1)+f(n-2);
        }
    }
}

9.通过递归算法计算5的阶乘。

代码如下(示例):

public class DiGuiDemo01 {
    public static void main(String[] args) {
        int result =jc(5);
        System.out.println(result);
    }

    public static int jc(int n){
        if(n==1){
            return 1;
        }else{
            return n*jc(n-1);
        }
    }
}

10.通过递归算法获取指定路径下的文件夹。

代码如下(示例):

public class DiGuiDemo02 {
    public static void main(String[] args) {
        File srcFile = new File("指定路径");

        getAllFilePath(srcFile);
    }


    //定义方法,用于获取给定目录下的所有内容
    public static void getAllFilePath(File srcFile){
        //获取给定的File数组,得到每一个File对象
        File[] fileArray = srcFile.listFiles();
        //遍历该File数组,得到每一个File对象
        if(fileArray != null){
            for(File file:fileArray){
                //判断File对象是否是目录
                if(file.isDirectory()){
                    getAllFilePath(file);
                }else{
                    System.out.println(file.getAbsolutePath());
                }
            }
        }
    }
}

总结

每天做一些基础的编程,逐步提升自己的编程能力,对未来更好地学号一门语言有着至关重要的作用。我将会每天持续更新一些基础的代码,供初学者联系使用。谢谢支持~~

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值