题一:
package com.heima.fan;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Random;
import java.util.TreeSet;
public class ti2 {
public static void main(String[] args) {
Random r = new Random();
StringBuilder sb = new StringBuilder();
StringBuilder sb1 = new StringBuilder();
TreeSet<Integer> ts = new TreeSet<>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
int num = o2 - o1;
return num == 0 ? 1 : num;
}
});
while(ts.size() < 10){
int a = r.nextInt(50) + 1;
if(a % 2 == 0){
ts.add(a);
}
}
for (Integer integer : ts) {
sb.append(integer).append(",");
}
sb.insert(0, "(");
sb.insert(sb.length()-1, ")");
String s = sb.substring(0, sb.length()-1);
System.out.println(s);
System.out.println("------------------------------------");
Iterator<Integer> it = ts.iterator();
int i = 0;
while(it.hasNext()){
int a = it.next();
sb1.append(a).append(",");
}
sb1.insert(0, "(");
sb1.insert(sb.length()-2, ")");
String s1 = sb1.substring(0, sb1.length()-1);
System.out.println(s1);
}
}
题二:
package com.heima.test;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class Test2 {
public static void main(String[] args) throws IOException {
String max = "上海传智播客,上海黑马,武汉传智播客,深圳黑马,北京传智播客,广州黑马,北京黑马";
int a =Demo1(max);
int b = Demo2(max);
StringBuilder sb = new StringBuilder();
sb.append("传智博客").append("=").append(a).append(",").append("黑马").append("=").append(b);
BufferedWriter bw = new BufferedWriter(new FileWriter("times.txt"));
for (int i = 0; i < sb.length(); i++) {
bw.write(sb.charAt(i));
}
bw.close();
}
public static int Demo2(String max) {
String min1 = "黑马";
int count = 0;
int index =0;
while((index = max.indexOf(min1)) != -1){
count ++;
max = max.substring(index + min1.length());
}
return count;
}
private static int Demo1(String max) {
String min1 = "传智播客";
int count = 0;
int index =0;
while((index = max.indexOf(min1)) != -1){
count ++;
max = max.substring(index + min1.length());
}
return count;
}
}
题3:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
public class Demo3 {
public static void main(String[] args) throws IOException {
File file = new File("D:\\");
String[] str = file.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
File file = new File(dir,name);
return file.getName().endsWith(".jpg") || file.getName().endsWith("png");
}
});
FileInputStream fis = null;
FileOutputStream fos = null;
for (String string : str) {
fis = new FileInputStream(file + string);
File file1 = new File("E:\\11",string);
fos = new FileOutputStream(file1);
int a;
while((a = fis.read()) != -1){
fos.write(a);
}
}
fis.close();
fos.close();
}
}
不知道写的符不符合楼主的要求,我想说大晚上的写题不容易啊 |