![](/img/trans.png)
[英]Why am I getting a “java.util.NoSuchElementException”?
[英]Why do I have this java.util.NoSuchElementException?
如果我使用Scanner調用類成員,並且以前在main方法中使用過Scanner,即使所有輸入均可用,也會出現NoSuchElementException錯誤。
import java.util.Scanner;
import java.io.*;
class Reminder{
static void computeReminder(){
Scanner S=new Scanner(System.in);
int a,b;
a=S.nextInt();
b=S.nextInt();
int reminder = a%b;
System.out.println(reminder);
}
}
class TestClass{
public static void main(String args[]){
Scanner scan=new Scanner(System.in);
int T=scan.nextInt(); //Getting no.of TestCases
for(int i=0; i<T; i++){
Reminder.computeReminder(); //Calling Class Method to find reminder.
}
}
}
我可以使用“ Reminder.computeReminder();” 和“ int T = scan.nextInt();” 分開但不一起。
我使用的輸入:
5
19 5
73 4
7 3
18 4
68 2
重要條件:computeReminder()方法應沒有參數,因為這是我學院的在線編譯器給出的條件。
嘗試重用掃描儀,而不要創建多個掃描儀。
class Reminder{
static void computeReminder(Scanner s){
int a,b;
a=s.nextInt();
b=s.nextInt();
int reminder = a%b;
System.out.println(reminder);
}
}
class TestClass{
public static void main(String args[]){
Scanner scan=new Scanner(System.in);
int T=scan.nextInt(); //Getting no.of TestCases
for(int i=0; i<T; i++){
Reminder.computeReminder(scan); //Calling Class Method to find reminder.
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.