让用户输入加法表达式

package com.StringExcrise;

import java.util.Scanner;

public class demo04 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("请输入表达式");
String m = s.next();//6+2 + 1;
m = m.replace(" ","");
String[] ss = m.split("\\+");
int sum = 0;
for (int i = 0; i <ss.length ; i++) {
int a = Integer.parseInt(ss[i]);
sum += a;
}
System.out.println(sum);

}
}
posted @ 2022-05-14 20:31  小松2739  阅读(15)  评论(0编辑  收藏  举报