博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java实现计算器
阅读量:5148 次
发布时间:2019-06-13

本文共 4438 字,大约阅读时间需要 14 分钟。

 

Main_business.java

import java.util.Scanner;

public class Main_business {

public void getMyCalculate() throws Exception {
Check_input myService = new Check_input();
System.out.println("请输入数字1");
//传入正则表达式
myService. incoming_rules("^([+][0-9][0-9]*|[0-9][0-9]*)+(.[0-9]{1,4})?");
String sDigital = myService.Num_Service();
float fDigital = Float.parseFloat(sDigital);
System.out.println("请输入符号");
String bSymbol = myService.operator_Service();
Digital_information execution_method = new OperationFactory().createOperation(bSymbol);
System.out.println("请输入数字2");
myService. incoming_rules("^([1-9][0-9]*)+(.[0-9]{1,4})?");
String sDigital2 = myService.Num_Service();
float fDigital2 = Float.parseFloat(sDigital2);
execution_method.setpDigital1(fDigital);
execution_method.setpDigital2(fDigital2);
float result = execution_method.getResult();
System.out.println("执行结果"+result);
System.out.println("继续操作请按0,退出请按任意键");
Scanner sc = new Scanner(System.in);
String option = sc.nextLine();
if(option.equals("0")) {
new Main_business().getMyCalculate();
}else {
System.exit(0);
}
}
}

Digital_information.java

public class Digital_information implements ResultHandler{

protected float pDigital1 ;
protected float pDigital2;
protected String pOperation;

public float getpDigital1() {

return pDigital1;
}

public void setpDigital1(float pDigital1) {

this.pDigital1 = pDigital1;
}

public float getpDigital2() {

return pDigital2;
}

public void setpDigital2(float pDigital2) {

this.pDigital2 = pDigital2;
}

public String getpOperation() {

return pOperation;
}

public void setpOperation(String pOperation) {

this.pOperation = pOperation;
}

@Override

public float getResult() {
// TODO Auto-generated method stub
return 0;
}
}

 OPerationFactory.java

public class OperationFactory {

public Digital_information createOperation(String operator) {

// TODO Auto-generated method stub

Digital_information ope = null;
switch (operator) {
case "+":
ope = new AddResult();
break;
case "-":
ope = new Subtraction();
break;
case "*":
ope = new MultiplyResult();
break;
case "/":
ope = new DevideResult();
break;
default:
break;
}
return ope;
}

}

ResultHandler.java

public interface ResultHandler {

public float getResult();
}

AddResult.java

 

public class AddResult extends Digital_information{

float pDigital;
//重写父类里面的方法
public float getResult() {
// TODO Auto-generated method stub
pDigital1 = super.getpDigital1();
pDigital2 = super.getpDigital2();
pDigital = pDigital1+pDigital2;
return pDigital;
}

 

}

DevideResult.java

 

public class DevideResult extends Digital_information{

float pDigital;
public float getResult() {
// TODO Auto-generated method stub
pDigital1 = super.getpDigital1();
pDigital2 = super.getpDigital2();
pDigital = pDigital1/pDigital2;
return pDigital;
}
}

MultiplyResult.java

 

public class MultiplyResult extends Digital_information{

float pDigital;
public float getResult() {
// TODO Auto-generated method stub
pDigital1 = super.getpDigital1();
pDigital2 = super.getpDigital2();
pDigital = pDigital1*pDigital2;
return pDigital;
}
}

Subtraction.java

 

public class Subtraction extends Digital_information{

float pDigital;
public float getResult() {
// TODO Auto-generated method stub
pDigital1 = super.getpDigital1();
pDigital2 = super.getpDigital2();
pDigital = pDigital1/pDigital2;
return pDigital;
}
}

 check_input.java

 

import java.util.Scanner;

import java.util.regex.Pattern;

import com.service.Exception.Check_exception;

public class Check_input {

String rules;

Scanner sc = new Scanner(System.in);
public String Num_Service() throws Exception {
String sDigital;
do {
try {
sDigital = sc.nextLine();
Pattern pattern = Pattern.compile(rules);//返回一个Pattern的对象。
boolean check_num2 = pattern.matcher(sDigital).matches();
if (check_num2==true) {
return sDigital;
}
else {
System.out.println("输入不正确,请重新输入");
//sDigital = sc.nextLine();
}
} catch (Exception e) {
System.out.println("请输入正确格式");
}
} while (true);
}
public String operator_Service() throws Check_exception {
String operator;
do {
try {
operator = sc.nextLine();
if (operator.equals("+")||operator.equals("-")||operator.equals("*")||operator.equals("/")) {
return operator;
}
else {
System.out.println("输入不正确,请重新输入");
//sDigital = sc.nextLine();
}
} catch (Exception e) {
System.out.println("请输入正确格式");
}
} while (true);
}
//接收前面出入的规则
public void incoming_rules(String rules) {
// TODO Auto-generated method stub
this.rules=rules;
}
}

 

转载于:https://www.cnblogs.com/zhulina-917/p/9679654.html

你可能感兴趣的文章
Python 正则表达式入门(初级篇)
查看>>
python学习之三 scrapy框架
查看>>
[转帖]Docker常用命令总结
查看>>
多线程GCD-牛逼中央调度器
查看>>
找水王
查看>>
牛客练习赛33 E tokitsukaze and Similar String (字符串哈希hash)
查看>>
专为多设备、多分辨率应用而设计
查看>>
51nod 1298 圆与三角形
查看>>
day 7-7 线程池与进程池
查看>>
VALSE2019总结(6)-年度总结-Re-ID
查看>>
luogu_1414 又是毕业季II
查看>>
List
查看>>
图Graph
查看>>
linux文件截取前几行,后几行,中间几行命令
查看>>
iOS 10中如何搭建一个语音转文字框架
查看>>
百度智能手环方案开源(含源码,原理图,APP,通信协议等)
查看>>
设计模式(三十一)------23种设计模式(23):简单工厂模式
查看>>
9.12日学习笔记
查看>>
spring-data-neo4j 4.2.4release文档概要
查看>>
0049-学校的上网费
查看>>