大致是这样的三道题作者: feng_pump 时间: 2016-10-5 16:45
1.这题容易,一个random函数,六次取值放到集合中,然后Collections.sort排个序,去掉第一个和最后一个,再把剩下的遍历相加求平均数即可
2.这个题的话考到了线程通信,有点坑爹,因为当时老师说不考的,所以班上好多人没做出来,记住线程间通信的格式你就没问题
3.创建方法后,在方法中进行复制的动作,listfiles获取文件,然后遍历,判断一下复制过去就可以,再通过一个集合,把符合条件的文件类型名字(avi)放进去再遍历集合就ok作者: feng_pump 时间: 2016-10-5 16:47
这是我的三道题代码,大家要是不会的看看,会的话可以指点指点{:2_32:}共同进步
1.public static void main(String[] args) {
ArrayList<Integer> al = new ArrayList<>();
Random r = new Random();
System.out.println("评委打分分别为:");
for(int i = 0 ; i < 6 ; i++){
int x = r.nextInt(10) + 1;
al.add(x);
System.out.print(x + "分 ");
}
System.out.println();
Collections.sort(al);
al.remove(0);
al.remove(al.size() - 1);
double sum = 0 ;
for(Integer i : al){
sum = sum + i ;
}
double count = sum/al.size();
System.out.println("该选手最终得分为:" + count + "分");
}
2.package test;
public class Test2 {
public static void main(String[] args) {
final Jiaoti j = new Jiaoti();
new Thread() {
public void run() {
for (int i = 0; i < 26; i++) {
try {
j.print1();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
new Thread() {
public void run() {
for (int i = 0; i < 26; i++) {
try {
j.print2();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
}
class Jiaoti {
static int flag = 1;
public static int num = 1;
public static char ch = 'A';
public void print1() throws InterruptedException {
synchronized (this) {
if (flag != 1) {
this.wait();
}
for (int i = 0; i < 2; i++) {
System.out.print(num++);
}
flag = 2;
this.notify();
}
}
public void print2() throws InterruptedException {
synchronized (this) {
if (flag != 2) {
this.wait();
}
char c = (char) ch++;
System.out.print(c);
flag = 1;
this.notify();
}
}
}
3.public static void main(String[] args) throws IOException {
String oldPath = "d:\\aaa";
String newPath = "d:\\bbb";
String fileFormat = "avi";
Test3.copy(oldPath, newPath, fileFormat);
ArrayList<String> al = new ArrayList<>();
for(File f : new File(oldPath).listFiles()){
if(f.isFile() && f.getName().endsWith(fileFormat)){
al.add(f.getName());
}
}
for(String s : al){
System.out.println(s);
}
}
public static void copy(String oldPath, String newPath, String fileFormat) throws IOException{
File file = new File(oldPath);
File[] fils = file.listFiles();