Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中使用EOF(不使用文件)_Java - Fatal编程技术网

如何在java中使用EOF(不使用文件)

如何在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

在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和
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 ...
          }
      }