java简易计算器实现(web)
一,web版
环境:tomcat8,jdk1.8,STS,java ee
Java Web,是用Java技术来解决相关web互联网领域的技术栈。web包括:web服务端和web客户端两部分。Java在客户端的应用有Java Applet,不过使用得很少,Java在服务器端的应用非常的丰富,比如Servlet,JSP、第三方框架等等。Java技术对Web领域的发展注入了强大的动力。
Calculator_Interface.java
package Pojo;
/*
*@author: CaoPengCheng
*@data:2020-4-4
*@version 1.0
*@see Calculator
*/
public interface Calculator_Interface {
public int jia(int... a);
public double jia(double... a);
public int jian(int... a);
public double jian(double... a);
public int chen(int... a);
public double chen(double... a);
public double chu(int... a);
public double chu(double... a);
}
Calculator.java
package Pojo;
import java.util.Scanner;
/*
*@author: CaoPengCheng
*@data:2020-4-4
*@version 1.0
*@see Calculator_Interface,CalilatorServlet
*/
public class Calculator implements Calculator_Interface {
private static Scanner sc = new Scanner(System.in);
@Override
public int jia(int... a) {
int sum = 0;
for (int x : a)
sum += x;
return sum;
}
@Override
public double jia(double... a) {
double sum = 0;
for (double x : a)
sum += x;
return sum;
}
@Override
public int jian(int... a) {
int sum = a[0];
for (int i = 1; i < a.length; i++)
sum = sum - a[i];
return sum;
}
@Override
public double jian(double... a) {
double sum = a[0];
for (int i = 1; i < a.length; i++)
sum = sum - a[i];
return sum;
}
@Override
public int chen(int... a) {
int sum = 1;
for (int x : a)
sum *= x;
return sum;
}
@Override
public double chen(double... a) {
double sum = 1.0f;
for (double x : a)
sum *= x;
return sum;
}
@Override
public double chu(int... a) {
double sum = a[0];
for (int i = 1; i < a.length; i++)
sum = sum / a[i];
return sum;
}
@Override
public double chu(double... a) {
double sum = a[0];
for (int i = 1; i < a.length; i++)
sum = sum / a[i];
return sum;
}
public static int inputInt() {
int a = sc.nextInt();
return a;
}
public static String inputString() {
return sc.next();
}
public static void scannerClose() {
sc.close();
}
}
Suan.java
package Suan;
import Pojo.Calculator;
/*
*@author: CaoPengCheng
*@data:2020-4-4
*@version 1.0
*@see Calculator,CalilatorServlet
*/
public class Suan {
public static double suan(String s, int... data) {
Calculator calculator = new Calculator();
double d = 0;
switch (s) {
case "+":
d = calculator.jia(data);
break;
case "-":
d = calculator.jian(data);
break;
case "*":
d = calculator.chen(data);
break;
case "/":
d = calculator.chu(data);
break;
}
return d;
}
}
Calculator.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>计算器</title>
<style type="text/css">
.h1{
text-align: center;
font-size: 25px;
}
.error{
margin-top: 50px;
margin-left:50px;
text-align: center;
font-size: 30px;
color: red;
}
.img{
float:left;
}
.content{
width: 670px;
height:470px;
margin-left:100px;
text-align: center;
/* border: 1px solid red; */
float:right;
}
.c1{
margin-top: 50px;
margin-left:50px;
text-align: center;
}
.yun{
margin-top: 20px;
margin-left:70px;
text-align: center;
}
.c2{
margin-top: 20px;
margin-left:50px ;
text-align: center;
}
.jie{
margin-top: 20px;
margin-left:110px;
text-align: center;
}
.t{
margin-top: 20px;
margin-left:50px;
text-align: center;
font-size: 30px;
color: green;
}
.b1{
margin-top: 50px;
text-align: center;
}
.b2{
margin-top: 10px;
text-align: center;
}
</style>
</head>
<body>
<div class="h1"><h1>---谁也不服计算器---</h1></div>
<div class="ddd">
<div class="img"><img alt="" style="width:450px;height:330px;" src="cxy.jpg"></div>
<div class="content">
<form action="Calulator" method="post">
<br>
<div class="error">${error}</div>
<div class="c1">
<label for="c1">输入第一个数:</label>
<input style="width: 320px;height: 30px" value="${c1}" type="text" name="c1" >
</div>
<div class="yun">
<label for="yun">选择运算符:</label>
<select name="yun" style="width: 320px;height: 30px">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
</div>
<div class="c2">
<label for="c2">输入第二个数:</label>
<input style="width: 320px;height: 30px" value="${c2}" type="text" name="c2" >
</div>
<div class="jie">
<label for="c2">结果:</label>
<input style="width: 320px;height: 30px" value="${jie}" type="text" name="结果" ><br>
</div>
<input style="width: 180px;height: 55px" type="submit" value="计算" name="sum" class="t">
</form>
</div>
</div>
<br>
<div class="b1"><h3>@CaoPengCheng</h3></div>
<div class="b2"><h1>永远相信美好的事情即将发生</h1></div>
</body>
</html>
CalilatorServlet.java
package Service;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Suan.Suan;
/*
*@author: CaoPengCheng
*@data:2020-4-4
*@version 1.0
*@see Calculator,HttpServlet
*/
@WebServlet("/Calulator")
public class CalilatorServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("GBK");
String s1 = req.getParameter("c1");
String s2 = req.getParameter("c2");
String yun = req.getParameter("yun");
double jie = 0;
boolean f = true;
int c1 = Integer.parseInt(s1);
int c2 = Integer.parseInt(s2);
String error = "分母为0!!!";
if (yun.equals("/") && c2 == 0) {
f = false;
req.setAttribute("error", error);
}
if (f) {
jie = Suan.suan(yun, c1, c2);
req.setAttribute("jie", jie);
}
req.setAttribute("c1", c1);
req.setAttribute("c2", c2);
req.getRequestDispatcher("/Calulator.jsp").forward(req, resp);
}
}
测试
二,控制台版
Calculator_Interface.java
package Calculator;
/*
*@author: CaoPengCheng
*@data:2020-4-4
*@version 1.0
*@see Calculator
*/
public interface Calculator_Interface {
public int jia(int... a);
public double jia(double... a);
public int jian(int... a);
public double jian(double... a);
public int chen(int... a);
public double chen(double... a);
public double chu(int... a);
public double chu(double... a);
}
Calculator.java
package Calculator;
import java.util.Scanner;
/*
*@author: CaoPengCheng
*@data:2020-4-4
*@version 1.0
*@see Calculator_Interface
*/
public class Calculator implements Calculator_Interface {
private static Scanner sc = new Scanner(System.in);
@Override
public int jia(int... a) {
int sum = 0;
for (int x : a)
sum += x;
return sum;
}
@Override
public double jia(double... a) {
double sum = 0;
for (double x : a)
sum += x;
return sum;
}
@Override
public int jian(int... a) {
int sum = a[0];
for (int i = 1; i < a.length; i++)
sum = sum - a[i];
return sum;
}
@Override
public double jian(double... a) {
double sum = a[0];
for (int i = 1; i < a.length; i++)
sum = sum - a[i];
return sum;
}
@Override
public int chen(int... a) {
int sum = 1;
for (int x : a)
sum *= x;
return sum;
}
@Override
public double chen(double... a) {
double sum = 1.0f;
for (double x : a)
sum *= x;
return sum;
}
@Override
public double chu(int... a) {
double sum = a[0];
for (int i = 1; i < a.length; i++)
sum = sum / a[i];
return sum;
}
@Override
public double chu(double... a) {
double sum = a[0];
for (int i = 1; i < a.length; i++)
sum = sum / a[i];
return sum;
}
public void Suan(String s, int... data) {
System.out.print("结果:");
switch (s) {
case "+":
System.out.println(data[0] + "" + s + data[1] + "" + "=" + this.jia(data));
break;
case "-":
System.out.println(data[0] + "" + s + data[1] + "" + "=" + this.jian(data));
break;
case "*":
System.out.println(data[0] + "" + s + data[1] + "" + "=" + this.chen(data));
break;
case "/":
System.out.println(data[0] + "" + s + data[1] + "" + "=" + this.chu(data));
break;
default:
System.out.println("错误!");
}
System.out.println("永远相信,美好的事情即将发生!");
}
public static int inputInt() {
int a = sc.nextInt();
return a;
}
public static String inputString() {
return sc.next();
}
public static void scannerClose() {
sc.close();
}
}
Calulator_Test .java
package Calculator;
import org.junit.Test;
/*
*@author: CaoPengCheng
*@data:2020-4-4
*@version 1.0
*@see Calculator
*/
public class Calulator_Test {
@Test
public void Test2() {
String s = "";
while (true) {
System.out.println("\n---谁也不服计算器---");
System.out.println("请输入一个数:");
int c1 = Calculator.inputInt();
System.out.println("请输入运算符:");
s = Calculator.inputString();
if (s.equals("over")) {
System.out.println("结束!");
break;
}
System.out.println("请输入二个数:");
int c2 = Calculator.inputInt();
if (c2 == 0) {
System.out.println("分母为0!!!\n重新开始!");
continue;
}
new Calculator().Suan(s, c1, c2);
}
Calculator.scannerClose();
}
public void Test1() {
Calculator_Interface c = new Calculator();
System.out.println("int jia=" + c.jia(1, 1));// 2
System.out.println("dou jia=" + c.jia(1.0, 1.0));// 2.0
System.out.println("int jian=" + c.jian(10, 5));// 5
System.out.println("dou jian=" + c.jian(20.0, 10.0));// 10.0
System.out.println("int chen=" + c.chen(2, 3));// 6
System.out.println("dou chen=" + c.chen(2.0, 3.0));// 6.0
System.out.println("int chu=" + c.chu(6, 2));// 3.0
System.out.println("dou chu=" + c.chu(6.0, 2.0));// 3.0
System.out.println(6.0 / 2.0);
}
}
测试
转载:https://blog.csdn.net/qq_41607217/article/details/115439272
查看评论