接受用户输入并创建一个包含两个参数的列表



我想让用户输入创建一个tasks列表,如下一个代码所示:

class Task {
private int start;
private int finish;
public Task(int start, int finish) {
this.start = start;
this.finish = finish;
}
public int getFinish() {
return finish;
}
public int getStart() {
return start;
}
@Override
public String toString() {
return "(" + getStart() + ", " + getFinish() + ")";
}
}

正如您所看到的,一个Task由两个int组成,一个用于开始,另一个用于结束。

然后我有一个类,它选择具有特定条件的任务,并返回任务列表:

class AS {
public static List<Task> activitySelector(List<Task> tasks) {
int k = 0;
Set<Integer> result = new HashSet<>();
result.add(0);
Collections.sort(tasks, Comparator.comparingInt(Task::getStart));
for (int i = 1; i < tasks.size(); i++) {
if (tasks.get(i).getStart() >= tasks.get(k).getFinish()) {
result.add(i);
k = i;
}
}
return result.stream().map(tasks::get).collect(Collectors.toList());
}

然后,我需要用户在终端中键入数字(开始、结束(来创建任务列表,但我一直纠结于如何获得用户输入并将每个输入按正确的方式排序。我知道我可以在本地申报名单,比如:

List<Task> tasks = Arrays.asList(new Task(1, 4), new Task(1, 3), 
new Task(2, 5), new Task(3, 7),
new Task(4, 7), new Task(6, 9), 
new Task(7, 8));

但我不知道如何根据用户输入创建List<Task>。你知道我该如何做到这一点吗?

public static List<Task> readInput() {
List<Task> tasks = new ArrayList<Task>();
Scanner sc = new Scanner(System.in);
int number_of_tasks = sc.nextInt();
for(int i = 0; i < number_of_tasks; i++) {
int start = sc.nextInt();
int finish = sc.nextInt();
tasks.add(new Task(start, finish));
}
return tasks;
}

你为什么不试试这个?

public static RuntimeException createException(String nextLine, RuntimeException exception) {
return new RuntimeException("invalid input line ["+ nextLine + "]; must be comma seperated start and finish times", exception);
}
public static List<Task> getTaskListUserInput() {
System.out.println("Please enter tasks as comma seperated start and finish times");
System.out.println("One task each line");
System.out.println("For example: 12,20");
System.out.println("Empty line will be treated as end of input");
System.out.println("-----------------");
List<Task> taskList = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
String nextLine = scanner.nextLine();
while (!nextLine.isEmpty()) {
String[] times = nextLine.split(",");
if (times.length != 2) {
throw createException(nextLine, null);
}
try {
int start = Integer.parseInt(times[0]);
int finish = Integer.parseInt(times[1]);
Task task = new Task(start, finish);
taskList.add(task);
} catch (ArrayIndexOutOfBoundsException | NumberFormatException exception) {
throw createException(nextLine, exception);
}
nextLine = scanner.nextLine();
}
System.out.println("-----------------");
return taskList;
}

用法:

public static void main(String[] args) {
List<Task> taskList = getTaskListUserInput();
System.out.println(taskList.size());
}
Please enter tasks as comma seperated start and finish times
One task each line
For example: 12,20
Empty line will be treated as end of input
-----------------
1,4
7,10
-----------------
2
Process finished with exit code 0

这里有一个使用BufferedReader的快速方法:

List<Task> tasks = new ArrayList<>();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
while (!((line = br.readLine()).isEmpty())){
StringTokenizer st = new StringTokenizer(line);
int startInt = Integer.parseInt(st.nextToken());
int endInt = Integer.parseInt(st.nextToken());
// Task is the same Task class you provided
tasks.add(new Task(startInt, endInt));
}
br.close();
System.out.println(tasks);

用户将每行输入两个用空格分隔的数字,最后,他们将输入一个空行。

示例输入

4 6
8 9

示例输出

[(4, 5), (6, 7)]

相关内容

最新更新