首页 > 代码库 > 四则运算
四则运算
package pers.weineel.quadratic;
import java.util.Scanner;
/**
* Created by weineeL on 2016/10/9.
*/
public class Counter {
public double doCount(double first, char operation, double second){
double result = 0;
switch (operation){
case ‘+‘: result = first + second; break;
case ‘-‘: result = first - second; break;
case ‘*‘: result = first * second; break;
case ‘/‘: if (second == 0) result = 0; else result = first / second; break;
default: result = 0; break;
}
return result;
}
public static void main(String [] argv){
Scanner scanner = new Scanner(System.in);
double first = scanner.nextDouble();
String operation = scanner.next();
double second = scanner.nextDouble();
// 示例 2 + 3 注意空格
Counter counter = new Counter();
System.out.println(counter.doCount(first, operation.charAt(0), second));
}
}
四则运算