import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = in.nextLine();
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int a = in.nextInt();
int b = in.nextInt();
System.out.println(a + b);
}
}
}

ACM模式模版

模版来源

public class Main {

public static void main(String[] args) {
//1.数据输入
Scanner in = new Scanner(System.in);
//读数字
int numLen = in.nextInt();
int[] numArr = new int[numLen];
int i = 0;
while(in.hasNextInt() && i < numLen){
numArr[i] = in.nextInt();
i++;
}
//读字符串
int strLen = in.nextInt();
in.nextLine(); //数字到字符串要换行
String[] strArr = new String[strLen];
//或者 strArr[] = in.nextLine().split(" ");
int j = 0;
while(in.hasNextLine() && j < strLen){
strArr[j] = in.nextLine();
j++;
}

//2. 处理
Solution solution = new Solution();
String result = solution.process(numArr, strArr);

//3. 输出
System.out.println(result);
//四舍五入输出小数
String str = String.format("%.2f",3.555);
System.out.println(str);
}
}

//下面类似 LeetCode 的核心代码模式
class Solution {
public String process(int[] nums, String[] strs) {
StringBuilder sb = new StringBuilder();
sb.append(Arrays.toString(nums));
sb.append(" && ");
sb.append(Arrays.toString(strs));
return sb.toString();
}
}

ACM模式下建立二叉树

package lc114_;

import java.util.*;

class TreeNode {
TreeNode left, right;
int val;

public TreeNode(int val) {
this.val = val;
}
}

public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] input = in.nextLine().split(",");
Integer[] output = new Integer[input.length];
for (int i = 0; i < input.length; i++) {
// 重点
if(input[i].equals("null")) {
output[i] = null;
}else{
output[i] = Integer.parseInt(input[i]);
}
}

LinkedList<Integer> inputList = new LinkedList<>(Arrays.asList(output));
TreeNode treeNode = buildTree(inputList);
System.out.println(preorderTraversal(treeNode));
}

public static TreeNode buildTree(LinkedList<Integer> inputList){
TreeNode node = null;
if (inputList ==null || inputList.isEmpty()){
return null;
}
Integer data = inputList.removeFirst();
if (data != null){
node = new TreeNode(data);
node.left = buildTree(inputList);
node.right = buildTree(inputList);
}
return node;
}

public static List<Integer> preorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
preorder(root, result);
return result;
}

public static void preorder(TreeNode root, List<Integer> result) {
if (root == null) {
return;
}
result.add(root.val);
preorder(root.left, result);
preorder(root.right, result);
}
}