如何在java中使用EOF(不使用文件)
在C中,我这样做:如何在java中使用EOF(不使用文件),java,Java,在C中,我这样做: int main(){ int N = 0; while(scanf("%d",&N) != EOF){ // <--- how to do this condition in java? ... Code ... } 工作正常。您有代码吗?Java不是C,而EOF在Java中通常用-1表示。您应该很快发现您没有在Java中使用scanf。从这里开始,看看你是否真的需要像EOF这样的东西。@ElliottFrisch实际上EOF通常由null表示,使用a和wh
int main(){
int N = 0;
while(scanf("%d",&N) != EOF){ // <--- how to do this condition in java?
... Code ...
}
工作正常。您有代码吗?Java不是
C
,而EOF在Java中通常用-1
表示。您应该很快发现您没有在Java中使用scanf
。从这里开始,看看你是否真的需要像EOF这样的东西。@ElliottFrisch实际上EOF通常由null
表示,使用a和while(scanner.hasNextLine())
和scanner.nextLine()
对行进行操作。
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
int N = 0;
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
while(scanner.hasNextLine()){
N = (int) scanner.nextDouble();
... code ...
}
}