练习题

1.第二题 已知有十六支男子足球队参加2008 北京奥运会。写一个程序,把这16 支球队随机分为4 个组。采用List集合和随机数

2008 北京奥运会男足参赛国家:

科特迪瓦,阿根廷,澳大利亚,塞尔维亚,荷兰,尼日利亚、日本,美国,中国,新西 兰,巴西,比利时,韩国,喀麦隆,洪都拉斯,意大利

提示:分配一个,删除一个

package wudil;


import java.util.*;

/**
 * Created by ttc on 18-1-10.
 */
public class WorldCupGroup {
    public static void main(String[] args) {
        Map> groupNum2Countrys = new HashMap<>();
        List stringList = new ArrayList<>();
        String strCountrys = "科特迪瓦,阿根廷,澳大利亚,塞尔维亚,荷兰,尼日利亚,日本,美国,中国,新西兰,巴西,比利时,韩国,喀麦隆,洪都拉斯,意大利";
        String[] countrys = strCountrys.split(",");
        for (String str : countrys) {
            stringList.add(str);
        }
        for (int j = 0; j < 4; j++) {
            List lstGroup = new ArrayList<>();
            for (int i = 0; i < 4; i++) {
                Random random = new Random();
                int index = random.nextInt(stringList.size());
                String selectCountry = stringList.get(index);
                lstGroup.add(selectCountry);
                stringList.remove(selectCountry);
            }
            groupNum2Countrys.put(j + 1, lstGroup);
        }
        for (Integer integer : groupNum2Countrys.keySet()) {
            System.out.println("第" + integer + "组");
            List lstCountrys = groupNum2Countrys.get(integer);
            for (String str : lstCountrys) {
                System.out.print(str + "\t");
                {
                    System.out.println();
                }
                // System.out.println();
            }
        }
        }
    }

2.用文件夹形式猜出文件夹里面有多少个单词?

首先在e盘建立一个words.txt文件,在里面把单词放在里面。开始运行
package exprerans;

import java.util.Random;
import java.util.Scanner;

/**
 * Created by ttc on 18-1-5.
 */
public class Guessworld {
    public static void main(String[] args) {
        String[] worlds = {"extends", "interface", "abstract", "implements", "throw", "orange", "student"};
        Random random = new Random();
        int index = random.nextInt(worlds.length);
        //  System.out.println(worlds[index]);
        char[] world = new char[worlds[index].length()];
        for (int i = 0; i < world.length; i++) {
            world[i] = '-';
            System.out.print(world[i]);
        }
        Scanner scanner = new Scanner(System.in);
        int time = 5;
        Guesstext guesstext = new Guesstext();
        while (true) {
            System.out.println();
            char userg = scanner.next().charAt(0);
            int num = worlds[index].indexOf(userg);
            if (num < 0) {
                time--;
                if (time == 0) {
                    break;
                }
                System.out.println("还剩" + time + "次机会");

                guesstext.src(world);
            } else {
                for (int i = 0; i < world.length; i++) {

                    if (userg == worlds[index].charAt(i)) {
                        world[i] = userg;
                    }

                }
                guesstext.src(world);
                System.out.println();
            }
            String sre = String.valueOf(world);
            if (!sre.contains("-")) {
                break;
            }

        }
        if (time > 0) {
            System.out.println("你赢了!");

        } else {
            System.out.println("你输了");
            System.out.println(worlds[index]);
        }
    }
}

练习3. 统计某个文件夹下所有java文件代码行数之和。查一共有多少代码

public class CodeLinesCount {

    public static int countJavaFileLines(String strFileName) throws IOException {
        FileReader fileReader = new FileReader("e:/mycode/" + strFileName);
        BufferedReader bufferedReader = new BufferedReader(fileReader);

        int count = 0;
        String strLine = bufferedReader.readLine();
        while (strLine != null)
        {
            count++;
            strLine = bufferedReader.readLine();
        }

        return count;
    }


    public static void main(String[] args) throws IOException {

        File file = new File("e:/mycode/");
        File[] files = file.listFiles();
        int totalCount = 0;
        for(File file1 : files)
        {
            System.out.println(file1.getName());
            int count = countJavaFileLines(file1.getName());
            totalCount += count;
        }

//        int count = countJavaFileLines("MyGuessWord.java");
        System.out.println(totalCount);
    }
}

4.写个程序读取以下学生信息文件,计算出每个学生总分,平均分,名次,写入到一个新文件中

学号 姓名   语文 数学 英语 平均值 总值  排序 
1   李守东   83  73  75 
2   徐贤坤   58  58  87 
3   钱云宋   41  86  90 
4   陈平     83  43  65 
5   金荣权   93  88  63 
6   陈如棉   99  93  43 
7   章可可   98  62  72 
8   陈伟奔   87  43  76 
9   张如祥   69  58  78 
10  丁尚游   80  56  57 
11  林宏旦   91  90  76 
12  曾上腾   100 96  54 
13  谢作品   82  100 55 
14  温从卫   73  46  101 
15  李明察   81  41  75 
16  彭鸿威   46  46  89 
17  翁文秀   57  43  58 
18  陈家伟   63  58  98 
19  温正考   100 64  57 
20  周文湘   50  50  79 
21  吴杰     65  65  83 
22  赖登城   60  79  53 
23  聂树露   51  76  45 
24  张雅琴   68  95  56 
25  曾瑞约   88  63  58 
26  王志强   96  79  78 
27  徐贤所   66  46  74 
28  陈祥枭   82  96  91 
29  温婷婷   41  73  96 
30  应孔余   66  81  71 
31  宋成取   71  68  62 
32  黄益省   65  56  43 
33  陈思文   55  100 44 
34  上官福新 64  62  70 
35  钟国横   49  69  56 
36  林型涨   78  73  50

需要生成的文件

学号  姓名  语文  数学  英语  平均值 总值  排序
1   李守东 83  73  75  77  231 9   
2   徐贤坤 58  58  87  67  203 21  
3   钱云宋 41  86  90  72  217 15  
4   陈平  83  43  65  63  191 29  
5   金荣权 93  88  63  81  244 5   
6   陈如棉 99  93  43  78  235 7   
7   章可可 98  62  72  77  232 8   
8   陈伟奔 87  43  76  68  206 19  
9   张如祥 69  58  78  68  205 20  
10  丁尚游 80  56  57  64  193 27  
11  林宏旦 91  90  76  85  257 2   
12  曾上腾 100 96  54  83  250 4   
13  谢作品 82  100 55  79  237 6   
14  温从卫 73  46  101 73  220 11  
15  李明察 81  41  75  65  197 25  
16  彭鸿威 46  46  89  60  181 31  
17  翁文秀 57  43  58  52  158 36  
18  陈家伟 63  58  98  73  219 12  
19  温正考 100 64  57  73  221 10  
20  周文湘 50  50  79  59  179 32  
21  吴杰  65  65  83  71  213 16  
22  赖登城 60  79  53  64  192 28  
23  聂树露 51  76  45  57  172 34  
24  张雅琴 68  95  56  73  219 13  
25  曾瑞约 88  63  58  69  209 18  
26  王志强 96  79  78  84  253 3   
27  徐贤所 66  46  74  62  186 30  
28  陈祥枭 82  96  91  89  269 1   
29  温婷婷 41  73  96  70  210 17  
30  应孔余 66  81  71  72  218 14  
31  宋成取 71  68  62  67  201 22  
32  黄益省 65  56  43  54  164 35  
33  陈思文 55  100 44  66  199 24  
34  上官新 64  62  70  65  196 26  
35  钟国横 49  69  56  58  174 33  
36  林型涨 78  73  50  67  201 23

代码:
学生类:

package com.company;

public class Student implements Comparable {
    private int sno;
    private String name;
    private int chinese_score;
    private int math_score;
    private int eng_score;
    private int avg_score;
    private int total_score;

    public int getEng_score() {
        return eng_score;
    }

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

    public int getSno() {
        return sno;
    }

    public void setSno(int sno) {
        this.sno = sno;
    }

    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 getMath_score() {
        return math_score;
    }

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

    public int getAvg_score() {
        return avg_score;
    }

    public void setAvg_score(int 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;
    }

    @Override
    public String toString() {
        return "Student{" +
                "sno=" + sno +
                ", name='" + name + '\'' +
                ", chinese_score=" + chinese_score +
                ", math_score=" + math_score +
                ", eng_score=" + eng_score +
                ", avg_score=" + avg_score +
                ", total_score=" + total_score +
                '}';
    }

    @Override
    public int compareTo(Object o) {
        Student student = (Student)o;
//        if(student.getSno() < this.getSno())
//        {
//            return 5;
//        }
//        else if(student.getSno() > this.getSno())
//        {
//            return -9;
//        }
//        else
//        {
//            return 0;
//        }

//        return this.getName().compareTo(student.getName());//打算按学生姓名排序
//        return this.getSno() - student.getSno();//打算按学生学号排序
          return student.getTotal_score() - this.getTotal_score();//打算按学生总分排序
    }
}

主类:

package com.company;

import java.io.*;
import java.util.*;

/**
 * Created by ttc on 2018/1/15.
 */
//        学号 姓名   语文 数学 英语 平均值 总值  排序
//        1   李守东   83  73  75
//        2   徐贤坤   58  58  87
//        3   钱云宋   41  86  90
//        4   陈平     83  43  65
//        5   金荣权   93  88  63

public class ComputeStudent {
    public static void main(String[] args) throws IOException {
        FileReader fileReader = new FileReader("e:/student_score.txt");
        BufferedReader bufferedReader = new BufferedReader(fileReader);

        String strStudentInfo = bufferedReader.readLine();//跳过头部
        List studentList = new ArrayList<>();
        strStudentInfo = bufferedReader.readLine();
        while(strStudentInfo != null)
        {

            String[] strings = strStudentInfo.split(" ");

            List stringList = new ArrayList();//保存的是一个学生的学号,姓名,三科成绩

            for(String str : strings)
            {
                if(!str.equals(""))
                {
                    stringList.add(str);
                }
            }

            //创建学生对象
            Student student = new Student();

            int sno = Integer.parseInt(stringList.get(0));
            student.setSno(sno);

            student.setName(stringList.get(1));
            int china_score = Integer.parseInt(stringList.get(2));
            student.setChinese_score(china_score);
            int math_score = Integer.parseInt(stringList.get(3));
            student.setMath_score(math_score);
            int eng_score = Integer.parseInt(stringList.get(4));
            student.setEng_score(eng_score);
            student.setTotal_score(china_score+math_score+eng_score);
            student.setAvg_score((china_score+math_score+eng_score)/3);

            studentList.add(student);

            strStudentInfo = bufferedReader.readLine();
        }

        System.out.println(strStudentInfo);

        FileWriter fileWriter = new FileWriter("e:/student_score_new.txt");
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);


        bufferedWriter.write("学号\t姓名\t语文\t数学\t英语\t平均值\t总值\t排序");
        bufferedWriter.newLine();


        List studentListOld = new ArrayList<>();
        studentListOld.addAll(studentList);//studentListOld里学生是按照学号排序

        Collections.sort(studentList);//studentList里学生是按照成绩排序

        //保存每个学生的名字和排名的键值对
        Map mapName2Rank = new HashMap<>();

        int index = 1;
        for(Student student : studentList)
        {
            mapName2Rank.put(student.getName(), index++);
        }

        //遍历list,将list里的每一个student对象写入到文件中
        for(Student student : studentListOld)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(student.getSno());
            stringBuilder.append("\t");
            stringBuilder.append(student.getName());
            stringBuilder.append("\t");
            stringBuilder.append(student.getChinese_score());
            stringBuilder.append("\t");
            stringBuilder.append(student.getMath_score());
            stringBuilder.append("\t");
            stringBuilder.append(student.getEng_score());
            stringBuilder.append("\t");
            stringBuilder.append(student.getAvg_score());
            stringBuilder.append("\t");
            stringBuilder.append(student.getTotal_score());
            stringBuilder.append("\t");

            String strStudentName = student.getName();
            int rank = mapName2Rank.get(strStudentName);//拿到了该名学生的名次

            stringBuilder.append(rank);
            stringBuilder.append("\t");

            bufferedWriter.write(stringBuilder.toString());
            bufferedWriter.newLine();
        }

        bufferedWriter.flush();

        fileReader.close();
        fileWriter.close();
        bufferedReader.close();
        bufferedWriter.close();
    }
}

5.打印一个日历本!

package com.company;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by ttc on 2018/1/16.
 */
public class CalendarDemo2 {
    public static void main(String[] args) throws ParseException {
        //求出该日期对应的月份一共有多少天
        //求出该日期对应的月份,第一天是星期几
        String strDate = "2017-09-23";

        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.DATE,1);//2018-01-16
        int day_of_week = calendar.get(Calendar.DAY_OF_WEEK);
//        System.out.println(day_of_week);////求出该日期对应的月份,第一天是星期几



        int[] array = new int[6*7];

        for(int i = 1; i <= day_counts; i++)
        {
            array[day_of_week - 1 + i - 1] = i;
        }

        System.out.print("日\t一\t二\t三\t四\t五\t六");
        System.out.println();
        for(int i = 0; i < array.length; i++)
        {
            if(array[i] == 0)
            {
                System.out.print(" ");
            }
            else
            {
                if(array[i] == day)
                {
                    System.out.print("*");
                }
                System.out.print(array[i]);
            }
            System.out.print("\t");
            if((i+1)%7==0)
            {
                System.out.println();
            }
        }

    }
}

日历简单一些改了一行代码

package com.company;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by ttc on 2018/1/16.
 */
public class CalendarDemo2 {
    public static void main(String[] args) throws ParseException {
        //求出该日期对应的月份一共有多少天
        //求出该日期对应的月份,第一天是星期几
        String strDate = "2017-09-23";

        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.DATE,1);//2017-08-01
        int day_of_week = calendar.get(Calendar.DAY_OF_WEEK);
//        System.out.println(day_of_week);////求出该日期对应的月份,第一天是星期几
        int[] array = new int[6*7];

        for(int i = 1; i <= day_counts; i++)
        {
            array[day_of_week - 1] = i;
            day_of_week++;
        }
        System.out.print("日\t一\t二\t三\t四\t五\t六");
        System.out.println();
        for(int i = 0; i < array.length; i++)
        {
            if(array[i] == 0)
            {
                System.out.print(" ");
            }
            else
            {
                if(array[i] == day)
                {
                    System.out.print("*");
                }
                System.out.print(array[i]);
            }
            System.out.print("\t");
            if((i+1)%7==0)
            {
                System.out.println();
            }
        }

    }
}

6.java多口链接,制作卖车票

第一个类

package com.company;

import java.util.Calendar;
import java.util.List;

/**
 * Created by ttc on 2018/1/17.
 */
public class TestSellTicket {
    public static void main(String[] args) throws InterruptedException {


        System.out.println(Thread.currentThread().getName()+"开始执行");
        WindowThread windowThread = new WindowThread();
        windowThread.setName("A窗口");
        windowThread.start();

        WindowThread windowThread2 = new WindowThread();
        windowThread2.setName("B窗口");
        windowThread2.start();

        WindowThread windowThread3 = new WindowThread();
        windowThread3.setName("C窗口");
        windowThread3.start();

        windowThread.join();
        windowThread2.join();
        windowThread3.join();

        System.out.println(Thread.currentThread().getName()+"停止执行");
//        System.out.println(Thread.currentThread().getName()+"开始执行");
//          WindowRunnable windowRunnable = new WindowRunnable();
//          Thread thread1 = new Thread(windowRunnable);
//          thread1.setPriority(Thread.MAX_PRIORITY);
//          Thread thread2 = new Thread(windowRunnable);
//        thread2.setPriority(Thread.MIN_PRIORITY);
//          Thread thread3 = new Thread(windowRunnable);
//        thread3.setPriority(Thread.MIN_PRIORITY);
//          thread1.start();
//          thread2.start();
//          thread3.start();
    }
}

第二个类

package com.company;

/**
 * Created by ttc on 2018/1/17.
 */
public class WindowThread extends Thread {
    int ticket_count = 100;
    @Override
    public void run()
    {
        while (true)
        {
            if(ticket_count > 0)
            {
                System.out.println(Thread.currentThread().getName() + "卖出了" + ticket_count);
                ticket_count--;
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            else
            {
                break;
            }
        }
    }
}

你可能感兴趣的:(练习题)