如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
import java.util.scanner; public class main { public static void main(string[] args) { scanner in = new scanner(system.in); while (in.hasnextline()) //判斷是否有輸入一行數(shù)據(jù) { string tmp = in.nextline(); //將一行數(shù)據(jù)讀出 if (tmp.equals( "q" )) //輸入q退出程序 break ; string arr[] = tmp.split( "\\s+" ); //使用正則表達式將字符串分割 “\\s+”表示多個空格 int sum = 0 ; for (string a:arr) //遍歷所有的字符串并轉(zhuǎn)換成整數(shù)求和 { sum+=integer.parseint(a); } system.out.println(sum); } } } |
以上這篇java讀取一行空格隔開的數(shù)字字符串并求出這些數(shù)字的和方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/qq_28584897/article/details/72730485