Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.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 在对一系列整数使用扫描程序时,如何跳过一些整数?_Java - Fatal编程技术网

Java 在对一系列整数使用扫描程序时,如何跳过一些整数?

Java 在对一系列整数使用扫描程序时,如何跳过一些整数?,java,Java,例如,下面的代码 while (lineScan.hasNextLine()) { int x = lineScan.nextInt(); x = lineScan.nextInt(); x = lineScan.nextInt(); x = lineScan.nextInt(); x = lineScan.nextInt(); System.out.println(x + "\n"); } 将每隔五个整数打印一次 有没有一种简单的方法可以跳过每五个整数?只

例如,下面的代码

while (lineScan.hasNextLine()) {
   int x = lineScan.nextInt();
   x = lineScan.nextInt();
   x = lineScan.nextInt();
   x = lineScan.nextInt();
   x = lineScan.nextInt();
   System.out.println(x + "\n");
}
将每隔五个整数打印一次

有没有一种简单的方法可以跳过每五个整数?

只需执行
nextInt()
并忽略结果?

只需执行
nextInt()
并忽略结果?

while(lineScan.hasNextLine()){
while (lineScan.hasNextLine()) {
    for(int i=0; i<5; i++)
        x = lineScan.nextInt();

    System.out.println(x + "\n");
}
对于(int i=0;i
while(lineScan.hasNextLine()){
对于(int i=0;i使用循环,我认为:

while(lineScan.hasNextLine()) {
     for(int i = 0; i < 4; i++) lineScan.nextInt();
     System.out.println(Integer.toString(lineScan.nextInt()) + "\n"); // is this supposed to be "popularity"?
}
while(lineScan.hasNextLine()){
对于(int i=0;i<4;i++)lineScan.nextInt();
System.out.println(Integer.toString(lineScan.nextInt())+“\n”);//这应该是“流行”吗?
}
我想使用循环:

while(lineScan.hasNextLine()) {
     for(int i = 0; i < 4; i++) lineScan.nextInt();
     System.out.println(Integer.toString(lineScan.nextInt()) + "\n"); // is this supposed to be "popularity"?
}
while(lineScan.hasNextLine()){
对于(int i=0;i<4;i++)lineScan.nextInt();
System.out.println(Integer.toString(lineScan.nextInt())+“\n”);//这应该是“流行”吗?
}
这样做可以:

int i = 0;
while(lineScan.hasNextLine()) {
   i++;
   int x = lineScan.nextInt();
   if (x%5 == 0) System.out.println(x + "\n");
}
这将有助于:

int i = 0;
while(lineScan.hasNextLine()) {
   i++;
   int x = lineScan.nextInt();
   if (x%5 == 0) System.out.println(x + "\n");
}

我看到很多人检查hasNextLine,然后读取int。我一直被教导,如果你检查hasNextX,你应该在检查通过后调用nextX,但决不能是nextY。换句话说,如果你检查hasNextLine(),你应该在nextLine()中读取,如果你想要int,你应该检查hasNextInt()在读取nextInt()之前,每次读取都要进行一次检查。在您的情况下,我会先读取该行,然后使用另一个仅在该行上工作的扫描程序对象对其进行操作(完成后不要忘记关闭它以节省资源!),或者使用字符串拆分

例如,如果用第一种方法,我会做如下事情:

  while (lineScan.hasNextLine()) {
     String line = lineScan.nextLine();

     Scanner innerScanner = new Scanner(line);
     int x = 0;
     while (innerScanner.hasNextInt()) {
        x = innerScanner.nextInt();
     }
     System.out.println("" + x);
     innerScanner.close();
  }
第二种方式是:

  while (lineScan.hasNextLine()) {
     String line = lineScan.nextLine();

     String[] splitLine = line.split(" "); // the delimiter may be different! a comma?
     if (splitLine.length >= 5) {
        System.out.println(splitLine[4]);
     }
  }

我看到很多人检查hasNextLine,然后读取int。我一直被教导,如果你检查hasNextX,你应该在检查通过后调用nextX,但决不能是nextY。换句话说,如果你检查hasNextLine(),你应该在nextLine()中读取,如果你想要int,你应该检查hasNextInt()在读取nextInt()之前,每次读取都要进行一次检查。在您的情况下,我会先读取该行,然后使用另一个仅在该行上工作的扫描程序对象对其进行操作(完成后不要忘记关闭它以节省资源!),或者使用字符串拆分

例如,如果用第一种方法,我会做如下事情:

  while (lineScan.hasNextLine()) {
     String line = lineScan.nextLine();

     Scanner innerScanner = new Scanner(line);
     int x = 0;
     while (innerScanner.hasNextInt()) {
        x = innerScanner.nextInt();
     }
     System.out.println("" + x);
     innerScanner.close();
  }
第二种方式是:

  while (lineScan.hasNextLine()) {
     String line = lineScan.nextLine();

     String[] splitLine = line.split(" "); // the delimiter may be different! a comma?
     if (splitLine.length >= 5) {
        System.out.println(splitLine[4]);
     }
  }

跳过每五个整数或打印每五个整数?跳过每五个整数或打印每五个整数?可能是因为您的代码根本没有编译?原语没有方法。
toString
是不必要的,因为
+
已经隐式地这样做了。另请参阅其他答案。@BalusC:我喜欢解释它。它有助于保持所有语言之间的一致性。可能是因为您的代码根本没有编译?原语没有方法。
toString
是不必要的,因为
+
已经隐式地这样做了。另请参见其他答案。@BalusC:我喜欢显式。它有助于保持所有语言之间的一致性。