首页 > 代码库 > 函数练习之计算机
函数练习之计算机
package day6HomeWork;
import javax.swing.JOptionPane;
/*
* 计算器(必须做):
实现加定义一个计算函数count,调用 add,减subtrace,乘ride,除except等函数,
main中调用count,并且传入opeator操作选择,进行加减乘除操作,并且选择退出的时候,
计算所有的计算结果。
*/
public class compute {
static double add(double a, double b){
return a+b;
}
static double subtrace(double a,double b){
return a-b;
}
static double ride(double a,double b){
return a*b;
}
static double except(double a,double b){
return a/b;
}
static void count(char opt,double a,double b){
switch (opt){
case ‘+‘:
JOptionPane.showMessageDialog(null, "你选择的是加法操作:");
JOptionPane.showMessageDialog(null,a+"+"+b+"="+add(a,b));
break;
case ‘-‘:
JOptionPane.showMessageDialog(null, "你选择的是减法操作:");
JOptionPane.showMessageDialog(null,a+"-"+b+"="+subtrace(a,b));
break;
case ‘*‘:
JOptionPane.showMessageDialog(null, "你选择的是乘法操作:");
JOptionPane.showMessageDialog(null,a+"*"+b+"="+ride(a,b));
break;
case ‘/‘:
JOptionPane.showMessageDialog(null, "你选择的是除法操作:");
JOptionPane.showMessageDialog(null,a+"/"+b+"="+except(a,b));
break;
case ‘0‘:
JOptionPane.showMessageDialog(null, "你选择了退出,将执行加减乘除所有操作:");
JOptionPane.showMessageDialog(null,a+"+"+b+"="+add(a,b));
JOptionPane.showMessageDialog(null,a+"-"+b+"="+subtrace(a,b));
JOptionPane.showMessageDialog(null,a+"*"+b+"="+ride(a,b));
JOptionPane.showMessageDialog(null,a+"/"+b+"="+except(a,b));
break;
default:
JOptionPane.showMessageDialog(null,"输入有误,请重新输入:");
break;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null,"请输入操作运算符和二个操作数:");
double a = Double.parseDouble(JOptionPane.showInputDialog(null,"请输入第一个数:"));
double b = Double.parseDouble(JOptionPane.showInputDialog(null,"请输入第二个数:"));
String str= JOptionPane.showInputDialog(null,"请输入操作符(+-*/(0代表退出,将执行所有操作)):");
char ch = str.toCharArray()[0];
count(ch,a,b);
}
}
函数练习之计算机