2018-06-07  本文已影响0人  十一_2bef

1.测试你活了多少天

public class hiredate {
    public static void main(String[] args) throws ParseException {
        System.out.println("请输入生日:");
        Scanner scanner = new Scanner(System.in);
        String myhiredate = scanner.nextLine();


        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date  = simpleDateFormat.parse(myhiredate);

        long mytime = date.getTime();
        System.out.println(mytime);

        Date date1 = new Date();
        long nowtime = date1.getTime();
        System.out.println(nowtime);

        long timecha = nowtime-mytime;

        System.out.println(timecha/1000/60/60/24);

    }
}

2.学生成绩表(主类)

public class ScoreSort {
    public static void main(String[] args) throws IOException {
        List<StudentInfo> studentInfoList = new ArrayList<>();

        FileReader fileReader = new FileReader("e:/student_info.txt");
        BufferedReader bufferedReader = new BufferedReader(fileReader);

        String header = bufferedReader.readLine();
        String string = bufferedReader.readLine();
        while (string != null)
        {
            String[] strArrays = string.split(" ");
            List<String> stringList = new ArrayList<>();
            for(int i = 0; i < strArrays.length;i++)
            {
                if(!strArrays[i].equals(""))
                {
                    stringList.add(strArrays[i].replace("\t",""));
                }
            }

            StudentInfo studentInfo = new StudentInfo();
            studentInfo.setNo(Integer.parseInt(stringList.get(0)));
            studentInfo.setName(stringList.get(1));
            studentInfo.setChinese_score(Integer.parseInt(stringList.get(2)));
            studentInfo.setMath_score(Integer.parseInt(stringList.get(3)));
            studentInfo.setEng_score(Integer.parseInt(stringList.get(4)));

            studentInfoList.add(studentInfo);


//            System.out.println(stringList);
            string = bufferedReader.readLine();
        }

//        System.out.println(studentInfoList);

        for(StudentInfo studentInfo : studentInfoList)
        {
            studentInfo.setTotal_score(studentInfo.getChinese_score()+studentInfo.getEng_score()+studentInfo.getMath_score());
            studentInfo.setAvg_score(studentInfo.getTotal_score() / 3);
        }

        List<StudentInfo> studentInfoListSort = new ArrayList<>();
        for(StudentInfo studentInfo : studentInfoList)
        {
            studentInfoListSort.add(studentInfo);
        }

        Collections.sort(studentInfoListSort);
        Map<Integer,Integer> map = new HashMap<>();//key学号,值是名次
        int index = 1;
        for(StudentInfo studentInfo : studentInfoListSort)
        {
            map.put(studentInfo.getNo(),index);
            index++;
        }
        for(StudentInfo studentInfo : studentInfoList)
        {
            int order = map.get(studentInfo.getNo());
            studentInfo.setSort(order);
        }

//        System.out.println(studentInfoList);

        FileWriter fileWriter = new FileWriter("e:/student_sort.txt");
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
        bufferedWriter.write(header);
        bufferedWriter.newLine();

        for(StudentInfo studentInfo : studentInfoList)
        {
            bufferedWriter.write(studentInfo.toString());
            bufferedWriter.newLine();
        }

        bufferedWriter.flush();

    }
}

学生成绩表(学生类)

public class StudentInfo implements Comparable {
    int no;
    String name;
    int chinese_score;
    int eng_score;
    int math_score;
    double avg_score;
    int total_score;
    int sort;

    @Override
    public String toString() {
//        return "StudentInfo{" +
//                "no=" + no +
//                ", name='" + name + '\'' +
//                ", chinese_score=" + chinese_score +
//                ", eng_score=" + eng_score +
//                ", math_score=" + math_score +
//                ", avg_score=" + avg_score +
//                ", total_score=" + total_score +
//                ", sort=" + sort +
//                '}';
        return no + "\t" + name + "\t" + chinese_score + "\t"
                + eng_score + "\t" + math_score + "\t" + avg_score + "\t" + total_score + "\t" + sort;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

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

    public int getChinese_score() {
        return chinese_score;
    }

    public void setChinese_score(int chinese_score) {
        this.chinese_score = chinese_score;
    }

    public int getEng_score() {
        return eng_score;
    }

    public void setEng_score(int eng_score) {
        this.eng_score = eng_score;
    }

    public int getMath_score() {
        return math_score;
    }

    public void setMath_score(int math_score) {
        this.math_score = math_score;
    }

    public double getAvg_score() {
        return avg_score;
    }

    public void setAvg_score(double avg_score) {
        this.avg_score = avg_score;
    }

    public int getTotal_score() {
        return total_score;
    }

    public void setTotal_score(int total_score) {
        this.total_score = total_score;
    }

    public int getSort() {
        return sort;
    }

    public void setSort(int sort) {
        this.sort = sort;
    }

    @Override
    public int compareTo(Object o) {
        StudentInfo studentInfo = (StudentInfo)o;
//        if(this.total_score == studentInfo.total_score)
//        {
//            return this.total_score - studentInfo.total_score;
//        }
//        else if(this.total_score > studentInfo.total_score)
//        {
//            return this.total_score - studentInfo.total_score;
//        }
//        else if(this.total_score < studentInfo.total_score)
//        {
//            return this.total_score - studentInfo.total_score;
//        }
        return studentInfo.total_score - this.total_score;
    }
}

3.日历

public class CalendarDemo {
    public static void main(String[] args) throws ParseException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入日期:格式为:yyyy-MM-dd");
        String strDate = scanner.nextLine();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = simpleDateFormat.parse(strDate);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        int day_counts = calendar.getActualMaximum(Calendar.DATE);
        int day = calendar.get(Calendar.DATE);

        calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),1);

        int first_day_of_month = calendar.get(Calendar.DAY_OF_WEEK);//6

        int[] array = new int[42];
        int index = 1;
        for(int i = first_day_of_month - 1; i < first_day_of_month - 1 + day_counts; i++)
        {
            array[i] = index;
            index++;
        }
        System.out.println("日\t一\t二\t三\t四\t五\t六");
        for(int i = 0; i < array.length; i++)
        {
            if(i % 7 == 0 && i != 0)
            {
                System.out.println();
            }

            if(array[i] != 0)
            {
                if(array[i] == day)
                {
                    System.out.print("*");
                }
                System.out.print(array[i]+ "\t");


            }
            else
            {
                System.out.print(""+ "\t");
            }

        }
//        System.out.println(Arrays.toString(array));

//       calendar.add(Calendar.MONTH,1);
//       int n = calendar.getActualMaximum(Calendar.DATE);
//        System.out.println(n);
//        calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),1);
//        System.out.println(calendar.get(Calendar.DAY_OF_WEEK));


    }
}

上一篇 下一篇

猜你喜欢

热点阅读