java简易计算器实现

avatar
avatar
云惠网小编
2845
文章
1
评论
2021年4月6日17:18:59 评论 9 次浏览 8137字阅读27分7秒
摘要

java简易计算器实现(web)一,web版环境:tomcat8,jdk1.8,STS,java eeJava Web,是用Java技术来解决相关web互联网领域的技术栈。web包括:web服务端和web客户端两部分。Java在客户端的应用有Java Applet,不过使用得很少,Java在服务器端的应用非常的丰富,比如Servlet,JSP、第三方框架等等。Java技术对Web领域的发展注入了强大的动力。Calculator_Interface.javapackage Pojo;[email protected]

测试

Calculator.jsp

Calculator.java

二,控制台版

Calculator.java

环境:tomcat8,jdk1.8,STS,java ee
Java Web,是用Java技术来解决相关web互联网领域的技术栈。web包括:web服务端和web客户端两部分。Java在客户端的应用有Java Applet,不过使用得很少,Java在服务器端的应用非常的丰富,比如Servlet,JSP、第三方框架等等。Java技术对Web领域的发展注入了强大的动力。

package Suan;
import Pojo.Calculator;
/*
[email protected]: CaoPengCheng
[email protected]:2020-4-4
[email protected] 1.0
[email protected] 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_Interface.java

package Pojo;
import java.util.Scanner;
/*
[email protected]: CaoPengCheng
[email protected]:2020-4-4
[email protected] 1.0
[email protected] 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();
}
}
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;
/*
[email protected]: CaoPengCheng
[email protected]:2020-4-4
[email protected] 1.0
[email protected] 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);
}
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

package Calculator;
import org.junit.Test;
/*
[email protected]: CaoPengCheng
[email protected]:2020-4-4
[email protected] 1.0
[email protected] 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);
}
}
<%@ 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>

Calculator_Interface.java

Calulator_Test .java

package Calculator;
import java.util.Scanner;
/*
[email protected]: CaoPengCheng
[email protected]:2020-4-4
[email protected] 1.0
[email protected] 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();
}
}

java简易计算器实现(web)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

Suan.java

package Calculator;
/*
[email protected]: CaoPengCheng
[email protected]:2020-4-4
[email protected] 1.0
[email protected] 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);
}
package Pojo;
/*
[email protected]: CaoPengCheng
[email protected]:2020-4-4
[email protected] 1.0
[email protected] 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);
}

测试

CalilatorServlet.java

一,web版

本文转自 https://blog.csdn.net/qq_41607217/article/details/115439272

腾讯云618
avatar
2w 字长文爆肝 JVM 经典面试题!太顶了! java

2w 字长文爆肝 JVM 经典面试题!太顶了!

如果你是中高级程序员,那我相信你一定被面试官问过JVM。下次再被问到JVM,你直接把老周的这篇文章丢给他吧!话不多说,让我们直接进入主题吧。JVM内存结构,常见异常,调优参数,调优...
JAVA初窥-DAY08 java

JAVA初窥-DAY08

JAVA初窥-DAY08面向过程与面向对象实例化及调用方法和成员变量面向过程与面向对象面向过程:注重的是某件事情过程中的每一个步骤的实现。面向对象:把面向过程中的每一个步骤交给一个...
腾讯云618
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: