在做笔试的时候,一般会有编程题目,这种编程题目有两种输入输出格式,一种是类似力扣的模式,给你提供好Solution类和相关的函数体,参数也是输入好的,只需要在函数内写好逻辑然后返回对应的结果就行;另一种是需要自己导入头文件,自己编写Main类和main函数,然后用scanner处理输入,输出也是要直接用print进行打印。

牛客网上关于acm输入输出格式的练习:

https://www.nowcoder.com/exam/test/78391990/submission?pid=27976983&pageSource=testHistory

处理输入:

  1. 使用 Scanner 类:Scanner 类是 Java 标准库中提供的用于读取用户输入的类。你可以使用 Scanner 类的不同方法来读取整数、浮点数、字符串等类型的输入数据。
    示例:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    import java.util.Scanner;

    public class Main {
    public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int num = scanner.nextInt(); // 读取整数
    double decimal = scanner.nextDouble(); // 读取浮点数
    String str = scanner.next(); // 读取字符串

    // 其他处理逻辑...
    }
    }

    当使用scanner.nextInt()读取完一个整数后,会停留在这一行的末尾,需要使用scanner.nextLine()读掉换行符,才能读取下一行。

    1
    2
    3
    4
    5
    6
    7
    int n = scanner.nextInt();
    scanner.nextLine(); // 读取第一行后的换行符
    String line = scanner.nextLine();

    //我们直接读取第一个整数 `n`,然后使用 `scanner.nextLine()` 读取第一个整数后的换行符。

    //接下来,我们使用 `scanner.nextLine()` 读取第二行的字符串。
  2. 使用 BufferedReader 类:BufferedReader 类提供了更高效的读取输入数据的方法。你可以使用 BufferedReader 类的 readLine() 方法来读取一行输入数据,然后根据需要进行解析和转换。
    示例:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;

    public class Main {
    public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    String line = br.readLine(); // 读取一行输入数据

    // 其他处理逻辑...
    }
    }

处理输出:

  1. 使用 System.out.println()System.out.println() 方法可以用于打印输出,并自动换行。
    示例:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    int num = 10;
    double decimal = 3.14;
    String str = "Hello";

    System.out.println(num); // 输出整数
    System.out.println(decimal); // 输出浮点数
    System.out.println(str); // 输出字符串

    // 10
    // 3.14
    // Hello
  2. 使用 System.out.print()System.out.print() 方法可以用于打印输出,但不会自动换行。
    示例:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    int num = 10;
    double decimal = 3.14;
    String str = "Hello";

    System.out.print(num); // 输出整数
    System.out.print(decimal); // 输出浮点数
    System.out.print(str); // 输出字符串

    // 103.14Hello

案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int a = in.nextInt();
int b = in.nextInt();
System.out.println(a + b);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] numbers = line.split(" ");
//如果一行是 10 11 12 13 14 15 这样的字符串,可以先读取一行再split

int sum = 0;
for (String number : numbers) {
sum += Integer.parseInt(number);
}

System.out.println(sum);
}

scanner.close();
}
}