輸入多對空格分開的兩個數a,b,輸出a+b的和,每一對數據的和占一行
我的應該符合了
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Test {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入數字,空格鍵分開:");
String stringNumbers = scanner.nextLine();
//String num[] = stringNumbers.split(" ");
int index = 0;
for(index = 0; index < stringNumbers.length(); index++){
if(stringNumbers.charAt(index) == ' ')
break;
}
int result= 0;
int num1 = stoi(stringNumbers.substring(0, index));
int num2 = stoi(stringNumbers.substring(index + 1, stringNumbers.length()));
String s = String.format("數字和為:%d", num1 + num2);
System.out.println(s);
}
public static int stoi(String s){
int res = 0;
for(int i = 0; i < s.length(); i++)
res = res * 10 + s.charAt(i) - '0';
return res;
}
}