作业一
package org.westos;
import java.io.File;
import java.io.IOException;
/**
* 递归删除带内容的目录 假设删除当前项目下的目录:demo,demo中可以有文件夹自己给出
* 验证在demo文件夹下既有文件和文件夹的情况下递归删除
* @author asus-pc
*
*/
public class TestDemo {
public static void main(String[] args) throws IOException {
File file = new File("demo\\aaa\\bbb\\ccc");
//创建目录
file.mkdirs();
/**
* 参数是file
* 先列举出所有包含文件
* isFile的话直接删除
* isDirectory的话调用方法继续删除这个目录
* file不包含任何文件时 删除
*/
//向demo文件夹内添加文件
File file2 = new File("demo\\a.txt");
File file3 = new File("demo\\aaa\\b.txt");
File file4 = new File("demo\\aaa\\bbb\\a.txt");
file2.createNewFile();
file3.createNewFile();
file4.createNewFile();
File file1 = new File("demo");
delete(file1);
}
private static void delete(File file) {
File[] list = file.listFiles();
//如果list的长度不为0 代表File内有文件存在 不可以直接删除
if(list.length != 0) {
for(File f : list) {
if(f.isFile()) {
//如果是个文件的话 直接删除
f.delete();
}else if(f.isDirectory()) {
//如果是文件夹的话 递归调用方法删除这个文件夹
delete(f);
}
}
}
//file文件夹下为空或者已经被删除完时,就把该文件夹删除
file.delete();
}
}
作业二
package org.westos;
import java.io.File;
import java.io.FilenameFilter;
/**
* 请大家把E:\JavaSE目录下所有的java结尾的文件的绝对路径给输出在控制台。
* @author asus-pc
*
*/
public class TestDemo2 {
public static void main(String[] args) {
File file = new File("e:\\JavaSE");
String[] list = file.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return new File(dir,name).isFile() && name.endsWith(".java");
}
});
for(String str : list) {
System.out.println(new File(str).getAbsolutePath());
}
}
}
作业三
package org.westos;
/**
* 在控制台上打印finally
* @author asus-pc
*
*/
public class TestDemo3 {
public static void main(String[] args) {
try {
return;
}finally {
System.out.println("finally");
}
}
}
作业四
package org.westos;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;
/**
* 1、请编写程序,统计一个字符串中每个字符出现的次数
* @author asus-pc
*
*/
public class TestDemo5 {
public static void main(String[] args) {
HashMap<Character, Integer> map = new HashMap<Character,Integer>();
Scanner sc = new Scanner(System.in);
//键盘录入一段字符串
System.out.println("请输入一段字符串");
String line = sc.nextLine();
//使用增强for循环遍历并统计字符出现次数
for (char c : line.toCharArray()) {
Set<Character> keySet = map.keySet();
if(keySet.contains(c)){
int value = map.get(c);
map.put(c, value + 1);
}else {
map.put(c, 1);
}
}
//输出
System.out.println(map);
}
}
作业五
package org.westos;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
/**
* 2、请编写程序,存储自定义对象到HashMap集合中,并采用两种方式遍历
* @author asus-pc
*
*/
public class TestDemo6 {
public static void main(String[] args) {
//创建存储自定义对象的集合
HashMap<Student,String> map = new HashMap<Student,String>();
//向集合中添加元素
map.put(new Student("张三",23), "s001");
map.put(new Student("李四",24), "s002");
map.put(new Student("王五",25), "s003");
map.put(new Student("赵六",26), "s004");
//方式一遍历
Set<Student> keySet = map.keySet();
for(Student s : keySet) {
System.out.println(map.get(s) + "---" + s.getName() + "---" + s.getAge());
}
//方式二遍历
System.out.println("----------------------");
for (Entry<Student, String> entry : map.entrySet()) {
System.out.println(entry.getValue() + "---" + entry.getKey());
}
}
}
作业六
package org.westos;
import java.util.Set;
import java.util.TreeMap;
import java.util.Map.Entry;
/**
* 3、请编写程序,存储自定义对象到TreeMap集合中,并采用两种方式遍历
* @author asus-pc
*
*/
public class TestDemo7 {
public static void main(String[] args) {
TreeMap<Student,String> map = new TreeMap<Student,String>();
//添加元素,打乱年龄的顺序
map.put(new Student("张三",23), "s001");
map.put(new Student("李四",24), "s002");
map.put(new Student("王五",27), "s003");
map.put(new Student("赵六",26), "s004");
//遍历TreeMap集合
//方式一
//方式一遍历
Set<Student> keySet = map.keySet();
for(Student s : keySet) {
System.out.println(map.get(s) + "---" + s.getName() + "---" + s.getAge());
}
//方式二遍历
System.out.println("----------------------");
for (Entry<Student, String> entry : map.entrySet()) {
System.out.println(entry.getValue() + "---" + entry.getKey());
}
}
}
作业七
package org.westos;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;import com.sun.javafx.collections.MappingChange.Map;
/**
* 5、请编写程序,完成模拟斗地主案例
* 基础版 —————— 仅实现发牌功能
*
* @author asus-pc
*
*/
public class TestDemo8 {
public static void main(String[] args) {
//初始化花色和牌
String[] colors = {"♠","♥","♦","♣"};
String[] cards = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
//创建牌盒
ArrayList<String> cardBox = new ArrayList<String>();
//向牌盒加入牌
for(String color : colors) {
for(String card : cards) {
cardBox.add(color + card);
}
}
cardBox.add("☀");
cardBox.add("☾");
//洗牌
Collections.shuffle(cardBox);
Collections.shuffle(cardBox);
Collections.shuffle(cardBox);
Collections.shuffle(cardBox);
//创建三个牌手
ArrayList<String> p1 = new ArrayList<String>();
ArrayList<String> p2 = new ArrayList<String>();
ArrayList<String> p3 = new ArrayList<String>();
//留出三张牌
ArrayList<String> card = new ArrayList<String>();
//给三个牌手发牌
/*
* 留出三张底牌,其余的牌按顺序发给三个牌手
*/
for(int i = 0;i < cardBox.size();i ++) {
if(i >= cardBox.size() - 3) {
card.add(cardBox.get(i));
}
if(i % 3 == 0 ) {
p1.add(cardBox.get(i));
}
if(i % 3 == 1) {
p2.add(cardBox.get(i));
}
if(i % 3 == 2 ) {
p3.add(cardBox.get(i));
}
}
System.out.println("p1:" + p1);
System.out.println("p2" + p2);
System.out.println("p3" + p3);
System.out.println("card" + card);
}
}
作业八
package org.westos;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;
/**
* 斗地主进阶版
* 分好牌后将牌排好序
* @author asus-pc
*
*/
public class TestDemo9 {
public static void main(String[] args) {
String[] colors = {"♠","♥","♦","♣"};
String[] cards = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
/*
* 如何实现对牌的排序?
* 每张牌的大小进行编号 存入HashMap中
* 注意因为编号要按顺序,所以不能用Set,要用ArrayList
* 每次按照编号发牌
*/
HashMap<Integer,String> map = new HashMap<Integer,String>();
int index = 0;
ArrayList<Integer> indexList = new ArrayList<Integer>();
//注意两个循环的顺序不能颠倒,因为排序是以数字的顺序来排
for(String card :cards) {
for(String color : colors) {
map.put(index, color+card);
indexList.add(index++);
}
}
//把大小王添加进去
map.put(index, "☾");
indexList.add(index++);
map.put(index, "☀");
indexList.add(index);
//洗牌
Collections.shuffle(indexList);
Collections.shuffle(indexList);
Collections.shuffle(indexList);
Collections.shuffle(indexList);
TreeSet<Integer> p1 = new TreeSet<Integer>();
TreeSet<Integer> p2 = new TreeSet<Integer>();
TreeSet<Integer> p3 = new TreeSet<Integer>();
TreeSet<Integer> card = new TreeSet<Integer>();
//发牌
for(int i = 0;i < indexList.size();i ++) {
if(i >= indexList.size() - 3) {
card.add(indexList.get(i));
}
if(i % 3 == 0) {
p1.add(indexList.get(i));
}
if(i % 3 == 1) {
p2.add(indexList.get(i));
}
if(i % 3 == 2) {
p3.add(indexList.get(i));
}
}
//遍历并查看牌
look("p1",map, p1);
look("p2",map, p2);
look("p3",map, p3);
look("card",map, card);
}
private static void look(String name,HashMap<Integer, String> map, TreeSet<Integer> p) {
System.out.print(name + ":");
for(Integer i : p) {
System.out.print(map.get(i) + " ");
}
System.out.println();
}
}