第四課:模型的使用
阿新 • • 發佈:2022-05-04
package javaSE.method;
import java.util.Scanner;
//計算器(待完善)(輸入的表示式字元間需要空格區分,只能計算一次,程式就結束了)
public class Demo07 {
public static void main(String[] args) {
math();
}
//整合演算法
public static double math(){
System.out.println("輸入需要計算的表示式(中間需空格分開)");
Scanner scanner = new Scanner(System.in);
double a = scanner.nextDouble();//獲取第一個數
char x = scanner.next().charAt(0);//charAt(0) 取第0號位的字元
double b = scanner.nextDouble();//獲取第二個數
double result = 0;
switch (x){
case '+':
result = add(a,b);
break ;
case '-':
result = reduce(a,b);
break;
case '*':
result = take(a,b);
break;
case '/':
result = division(a,b);
break;
}
System.out.println("結果為"+result);
scanner.close();
return result;
}
//加
public static double add(double a,double b){
double result = 0;
result = a + b;
return result;
}
//減
public static double reduce(double a,double b){
double result = 0;
result = a - b;
return result;
}
//乘
public static double take(double a,double b){
double result = 0;
result = a * b;
return result;
}
//除
public