Description
今年中秋节,大宝哥带着一盒月饼去看望小学数学老师。碰巧数学老师在指导他的学生“100以内的加减法”,由于老师要指导很多个小朋友,所以经常会忙不过来,于是老师便请大宝帮忙检查下小朋友们的作业情况,并统计出每个小朋友做对了几道题。其中每道算术题的格式为a+b=c、a-b=c、a+b=?、a-b=? 中的一种,最后的问号表示这个小朋友不会计算这道题。在检查作业的过程中,大宝发现他经常算错题目而且会数错个数。所以他想请你帮忙写个程序来统计小朋友做对题目的个数。
Input
输入包含多组测试数据,每组有一行,每行为一道加法或减法算式,数据格式保证符合上述格式,不包含任何其他字符且所有整数均不包含前导0。其中(0≤a,b≤100,0≤c≤200)。
Output
输出只有一行,包含一个整数,即等式成立的个数。
Sample
Input
2+2=3
3-1=2
6+7=?
99-0=?
Output
1
Hint
import java.util.Scanner;
class Maths {
String str;
public Maths(String str) {
this.str = str;
}
public boolean judge() {
boolean flag;
if(str.indexOf("-") >= 0)//用flag的值来判断,计算式是进行加法还是减法
flag = false;
else
flag = true;
String[] temp = str.split("\\D+");// \D表示非数字
if(temp.length < 3) {
return false;
}
int a = Integer.parseInt(temp[0]);
int b = Integer.parseInt(temp[1]);
int c = Integer.parseInt(temp[2]);
if(flag) {
if(a + b == c)
return true;
else
return false;
} else {
if(a - b == c)
return true;
else
return false;
}
}
}
public class Main {
public static void main(String args[]) {
Scanner reader = new Scanner(System.in);
String str;
int cnt = 0;
while(reader.hasNext()) {
str = reader.next();
Maths maths = new Maths(str);
if(maths.judge())
cnt++;
}
System.out.println(cnt);
reader.close();
}
}