进行数值统计的时候,可能会混入一些非数值的元素,下面请编程完成对输入序列进行求和统计并输出, 如果遇到非数值元素,则自动跳过,并在最终结果输出行之后另起一行,输出attention
输入格式:
单行输入,元素之间使用空格分开
输出格式:
对元素所对应的整数进行求和并输出
如果遇到非数值元素,则自动跳过,并在最终结果输出行之后另起一行,输出attention
输入样例a:
在这里给出一组输入。例如:
1 2 3 4 5
输出样例a:
在这里给出相应的输出。例如:
15
输入样例b:
在这里给出一组输入。例如:
1 2 3 4 a 5
输出样例b:
在这里给出相应的输出。例如:
15
attention
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int flag = 0 , sum=0;
String s = sc.nextLine();
String[] str = s.split(" ");
for (int i = 0; i < str.length; i++) {
try {
int num = Integer.valueOf(str[i]);
sum+=num;
}catch (Exception a) {
flag=1 ;
continue;
}
}
System.out.println(sum);
if(flag==1) {
System.out.println("attention");
}
}
}