初学Java
该段代码报错NoSuchElementException,原因应该是我使用了Ctrl + D结束输入并尝试了继续输入。我在输入Ctrl + D 后重新分配了scanner,但仍然报错,求解
System.out.print("请输入节点数量: ");
int nodeNum = sc.nextInt();
boolean[][] maps = new boolean[nodeNum][nodeNum];
System.out.println("请输入点之间的关系, 每行一对, 输入Ctrl + D 结束输入: " +
"\n例: 点A可以到达点B, 输入A B");
while(sc.hasNext()){
int a = sc.nextInt();
int b = sc.nextInt();
maps[a - 1][b - 1] = true;
}
sc = new Scanner(System.in);
System.out.print("请输入目标点。例:要求A到B的最短路,输入A B: ");
int start = sc.nextInt();
int end = sc.nextInt();
int ans = Search.BfsSerch.shortestPath(maps, start, end);
if(ans == -1){
System.out.println(start + "无法到达" + end);
}else{
System.out.println(start + "到达" + end + "的最短路长度为: " + ans);
}
sc.close();
测试数据:
第一次输入:5
第二次输入:
1 2
2 3
3 4
4 5
Ctrl + D
完整代码:
package test;
import java.util.*;
/**
* @author WitMoy
* @version V1.8
* @date : 2022-07-06 16:43
*/
public class Main {
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
// //快排测试
// long begin1 = System.currentTimeMillis();
// int[] arr = new int[]{1,6,9,0,4,7,5,8,8,2};
// Sort.QuickSort.sortAll(arr, 0, arr.length, false);
// System.out.println("排好序的数组: " + Arrays.toString(arr) + "\n");
// long end1 = System.currentTimeMillis();
// System.out.println("用时: " + (end1 - begin1) + "ms\n");
// //roundK测试
// long begin2 = System.currentTimeMillis();
// System.out.print("请输入目标位置: ");
// int rank = sc.nextInt();
// System.out.print("升序第" + rank + "请输入true,否则请输入false: ");
// boolean style0 = sc.nextBoolean();
// int t = Sort.QuickSort.largestK(arr, 0, arr.length, rank, style0);
// System.out.println("该目标值为: " + t + '\n');
// long end2 = System.currentTimeMillis();
// System.out.println("用时: " + (end2 - begin2) + "ms" +
// "\t(时长包含输入)\n");
// //二分测试
// long begin3 = System.currentTimeMillis();
// System.out.print("请输入在该数组中要搜索的目标值: ");
// int round = sc.nextInt();
// System.out.print("请输入您需要的数组排序规律(升序true,降序false): ");
// boolean style = sc.nextBoolean();
// int ans = Search.dichotomySerch(arr, arr.length, round, style);
// if(ans < 0) System.out.println("无法找到目标值");
// else System.out.println("该目标角标为: " + ans);
// long end3 = System.currentTimeMillis();
// System.out.println("用时: " + (end3 - begin3) + "ms" +
// "\t(时长包含输入)\n");
// //堆排测试
// int[] arr1 = new int[]{1,6,9,0,4,7,5,8,8,2};
// long begin4 = System.currentTimeMillis();
// System.out.print("寻找该数组前K小的数输入true,前K大输入false: ");
// boolean style1 = sc.nextBoolean();
// String tell = "";
// if(style1) tell = "小";
// else tell = "大";
// System.out.print("您要找前几" + tell + "的数?请输入: ");
// int target = sc.nextInt();
// int[] topK = Sort.HeapSort.topK(arr1, target, arr1.length, style1);
// System.out.println("前" + target + tell + "的数组: " + Arrays.toString(topK) + "\n");
// System.out.print("输入true为升序,false为降序。请输入排序方式:");
// boolean style2 = sc.nextBoolean();
// Sort.HeapSort.sortAll(arr1, arr1.length, style2);
// System.out.println("排好序的数组: " + Arrays.toString(arr1));
// long end4 = System.currentTimeMillis();
// System.out.println("用时: " + (end4 - begin4) + "ms" +
// "\t(时长包含输入)\n");
// int[] arr2 = new int[]{1,6,9,0,4,7,5,8,8,2};
// Sort.MergeSort.sort(arr2, 0, arr2.length);
// System.out.println("排好序的数组: " + Arrays.toString(arr2) + "\n");
//bfs测试
System.out.print("请输入节点数量: ");
int nodeNum = sc.nextInt();
boolean[][] maps = new boolean[nodeNum][nodeNum];
System.out.println("请输入点之间的关系, 每行一对, 输入Ctrl + D 结束输入: " +
"\n例: 点A可以到达点B, 输入A B");
while(sc.hasNext()){
int a = sc.nextInt();
int b = sc.nextInt();
maps[a - 1][b - 1] = true;
}
sc = new Scanner(System.in);
System.out.print("请输入目标点。例:要求A到B的最短路,输入A B: ");
int start = sc.nextInt();
int end = sc.nextInt();
int ans = Search.BfsSearch.shortestPath(maps, start, end);
if(ans == -1){
System.out.println(start + "无法到达" + end);
}else{
System.out.println(start + "到达" + end + "的最短路长度为: " + ans);
}
sc.close();
}
}
public static class BfsSearch{
public static int shortestPath(boolean[][] maps, int start, int end){
if(end == start) return 0;
int nodeNum = maps.length;
LinkedList<Integer> queue = new LinkedList<>();
queue.offer(start);
int ans = 0;
int queueInternalNum = 1, queueExternalNum = 0, popNum = 0;
boolean[] flag = new boolean[nodeNum];
while(ans < nodeNum){
ans++;
while(queueInternalNum > popNum){
int tailEnd = queue.poll();
popNum++;
for(int i = 0; i < nodeNum; i++){
if(maps[tailEnd - 1][i] && !flag[i]){
if(i == end - 1) {
return ans;
}
flag[i] = true;
queueExternalNum++;
queue.offer((i + 1));
}
}
}
queueInternalNum = queueExternalNum;
queueExternalNum = 0;
popNum = 0;
}
return -1;
}
}