qq_41811552
2018-03-26 16:07
采纳率: 66.7%
浏览 766
已采纳

java代码用eclipse编译异常!

package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

public class NumricRange {
public static void main(String... args) throws Exception {
int[] numrics = new int[1000];
File file = new File("range-file.txt");
PrintStream out = new PrintStream(file);
out.println("562 ~602 928 ~968 671 ~711 819 ~859 302 ~342 532 ~572 958 ~998");
out.println("453 ~493 671 ~711 193 ~233 931 ~971 54 ~94 349 ~389 775 ~815");
out.close();
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
String[] groups = line.split("[\t ]+(?!~)");
if (groups != null)
for (String group : groups) {
String[] ranges = group.split("[\t ]*~");
int min = Integer.parseInt(ranges[0]);
int max = Integer.parseInt(ranges[1]);
for (int i = min; i <= max; i++)
numrics[i]++;
}
}
Map> result = new TreeMap>();
int lastTimes = -1;
int[] lastResult = null;
for (int i = 0; i < 1000; i++) {
int time = numrics[i];
if (time == lastTimes)
lastResult[1] = i;
else if (time > -1) {
List list = result.get(time);
if (list == null) {
list = new ArrayList();
result.put(time, list);
}
list.add(lastResult = new int[] { i, i });
lastTimes=time;
}
}
Iterator>> it = result.entrySet().iterator();
while (it.hasNext()) {
Entry> entry = it.next();
int time = entry.getKey();
List list = entry.getValue();
System.out.println("出现" + time + "次的范围:");
for (int[] range : list) {
System.out.print(range[0] + "~" + range[1] + " ");
}
System.out.println();
}
}
}

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

6条回答 默认 最新

相关推荐 更多相似问题