import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner line = new Scanner(System.in);
int counter = 1;
while (line.hasNextLine()) {
String line = line.nextLine();
System.out.println(counter + " " + line);
counter++;
}
}
}
Run Code Online (Sandbox Code Playgroud)
任务:每一行将包含一个非空字符串。读取直到EOF。对于每一行,打印行号,后跟一个空格和行内容。
输入示例:
Hello world
I am a file
Read me until end-of-file.
Run Code Online (Sandbox Code Playgroud)
示例输出:
1 Hello world
2 I am a file
3 Read me until end-of-file.
Run Code Online (Sandbox Code Playgroud)
如果您想从文件进行扫描,可以使用以下代码。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) throws FileNotFoundException {
Scanner scan = new Scanner(new File("input.txt"));
int counter = 1;
while (scan.hasNextLine()) {
String line = scan.nextLine();
System.out.println(counter + " " + line);
counter++;
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
32530 次 |
最近记录: |