更新时间:2023-11-18 10:19:52
不要在每种方法中创建一个Scanner对象。传递您创建的第一个扫描仪对象。
Don't create a Scanner object in every method. Pass the first Scanner object you have created around.
以下是应该解决此问题的更改列表:
Here is a list of changes that should fix the issue:
--- demo-old.java 2012-01-25 23:12:54.000000000 +0530
+++ demo.java 2012-01-25 23:13:45.000000000 +0530
@@ -10,4 +10,3 @@
-void read_graph() {
- Scanner sc = new Scanner(System.in);
+void read_graph(Scanner sc) {
N = sc.nextInt();
@@ -26,4 +25,3 @@
-void query(){
-Scanner sc = new Scanner(System.in);
+void query(Scanner sc){
int P, Q;
@@ -53,4 +51,4 @@
while (numGraphs>0){
- G.read_graph();
- G.query();
+ G.read_graph(sc);
+ G.query(sc);
numGraphs--;