不好意思,我做了很久还是没做出来,应该是没有处理好如何结束程序
这里有一个我最近做的,不知道为什么还是没法通过
package org.firefrog.baidu.zhidao.java;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
List li = new ArrayList();
while (true) {
Scanner in = new Scanner(System.in);
String nextLine = in.nextLine();
int[] arr = getInt(nextLine);
if (arr == null) {
break;
}
int a = arr[0];
int b = arr[1];
li.add(a + b);
}
for (Integer i : li) {
System.out.println(i);
}
}
public static int[] getInt(String str) {
try {
str = str.trim();
int result[] = new int[2];
int count = 1;
String numstr = "";
for (int i = 0; i < str.length(); i++) {
if (count == 1) {
if (str.charAt(i) != ' ') {
numstr += str.charAt(i);
} else {
count = 2;
result[0] = Integer.parseInt(numstr);
numstr = "";
}
} else {
if (str.charAt(i) != ' ') {
numstr += str.charAt(i);
} else {
break;
}
}
}
result[1] = Integer.parseInt(numstr);
if (count == 1) {
return null;
}
return result;
} catch (Exception e) {
return null;
}
}
}