文章詳情頁
JSP實(shí)現(xiàn)簡單網(wǎng)頁計(jì)算器
瀏覽:12日期:2022-06-07 17:54:09
本文實(shí)例為大家分享了JSP實(shí)現(xiàn)簡單網(wǎng)頁計(jì)算器的具體代碼,供大家參考,具體內(nèi)容如下
一、構(gòu)造一個(gè)簡單的計(jì)算器,能夠進(jìn)行“+、—、*、/”運(yùn)算
(1)編寫jsp頁面,用戶通過表單輸入兩個(gè)操作數(shù)和運(yùn)算符,調(diào)用該頁面自身處理該表單,通過調(diào)用SimpleCalculator類的實(shí)例實(shí)現(xiàn)運(yùn)算邏輯,并顯示運(yùn)算結(jié)果。
(2)實(shí)現(xiàn)下邊的jsp網(wǎng)頁計(jì)算器:
二、代碼實(shí)現(xiàn)
(1)jsp頁面
<%@page import="com.beans.SimpleCalculator"%><%@ page language="java" contentType="text/html; charset=utf-8"? ? pageEncoding="utf-8"%><!DOCTYPE html><html><head><meta charset="utf-8"><title>計(jì)算器</title></head><body>? ? ? ? ? ?? ? ? ?<form action="" method="post">? ? ? ??? ??? ?第一個(gè)數(shù):<input type="text" value="" name="first" size="25px"/>? ? ? ??? ??? ?<br /><br />? ? ? ??? ??? ?第二個(gè)數(shù):<input type="text" value="" name="second" size="25px"/>? ? ? ??? ??? ?<br /><br />? ? ? ??? ??? ? ? ? ? ??? ??? ?<input type="submit" value="+" name="operator" size="3"/> ? ? ? ? ??? ??? ?<input type="submit" value="-" name="operator" size="3"/> ? ? ? ??? ??? ?<input type="submit" value="*" name="operator" size="3"/> ? ? ? ? ??? ??? ?<input type="submit" value="/" name="operator" size="3"/> ? ?? ??? ??? ??? ? ?? ??? ??? ??? ?<input type="reset" value="清除"/>? ? ? ?</form>? ? ? ? <br /><br />? ? ? ??? ??? ?<%? ? ? ??? ??? ?? ? ? ??? ??? ??? ??? ??? ?//獲取表單中的數(shù)據(jù)進(jìn)行運(yùn)算?? ??? ??? ??? ??? ??? ?String first = request.getParameter("first");//第一個(gè)數(shù)?? ??? ??? ??? ??? ??? ?String second = request.getParameter("second");//第二個(gè)數(shù)?? ??? ??? ??? ??? ??? ?String operator = request.getParameter("operator");//運(yùn)算符?? ??? ??? ??? ? ?? ??? ?String result = "" ;//運(yùn)算結(jié)果?? ??? ??? ??? ? ?? ??? ?? ? ? ??? ??? ??? ??? ??? ?//判斷運(yùn)算符? ? ? ??? ??? ??? ??? ??? ?if(operator.equals("+")) {? ? ? ??? ??? ??? ??? ??? ??? ?result = String.valueOf((Integer.valueOf(first) + Integer.valueOf(second) ));? ? ? ??? ??? ??? ??? ??? ?}? ? ? ??? ??? ??? ??? ??? ?if(operator.equals("-")) {? ? ? ??? ??? ??? ??? ??? ??? ?result = String.valueOf((Integer.valueOf(first) - Integer.valueOf(second) ));? ? ? ??? ??? ??? ??? ??? ?}? ? ? ??? ??? ??? ??? ??? ?if(operator.equals("*")) {? ? ? ??? ??? ??? ??? ??? ??? ?result = String.valueOf((Integer.valueOf(first) * Integer.valueOf(second) ));? ? ? ??? ??? ??? ??? ??? ?}? ? ? ??? ??? ??? ??? ??? ?if(operator.equals("/")) {? ? ? ??? ??? ??? ??? ??? ??? ?? ? ? ??? ??? ??? ??? ??? ??? ?if(second.equals("0")) {? ? ? ??? ??? ??? ??? ??? ??? ??? ?result = "除數(shù)不能為0";? ? ? ??? ??? ??? ??? ??? ??? ?}else {? ? ? ??? ??? ??? ??? ??? ??? ??? ?result = String.valueOf((double)(Integer.valueOf(first) / (double)Integer.valueOf(second) ));? ? ? ??? ??? ??? ??? ??? ??? ?}? ? ? ??? ??? ??? ??? ??? ?}? ? ? ??? ??? ??? ??? ??? ?? ? ? ??? ??? ??? ??? ??? ?//定義一個(gè)計(jì)算器類? ? ? ??? ??? ??? ??? ??? ?SimpleCalculator simpleCalculator = new SimpleCalculator();? ? ? ??? ??? ??? ??? ??? ?simpleCalculator.setResult(result);? ? ? ??? ??? ??? ??? ??? ?if( !simpleCalculator.getResult().equals("") && simpleCalculator.getResult() != null){? ? ? ??? ??? ??? ??? ??? ??? ?out.print("<h2 style= "color: blue">");? ? ? ??? ??? ??? ??? ??? ??? ?out.print("計(jì)算結(jié)果:"+first+operator+second+" = "+simpleCalculator.getResult());? ? ? ??? ??? ??? ??? ??? ??? ?out.print("<h4>");? ? ? ??? ??? ??? ??? ??? ?}else{? ? ? ??? ??? ??? ??? ??? ??? ?out.print("計(jì)算錯(cuò)誤");?? ? ? ??? ??? ??? ??? ??? ?}? ? ? ??? ??? ?? ? ? ??? ??? ??? ??? ?%>? ? ? ??? ??? ??? ??? ? ?<br /><br /></body></html>
(2)SimpleCalculator類
public class SimpleCalculator {?? ??? ?//定義變量?? ?private String first;//第一個(gè)數(shù)?? ?private String second;//第二個(gè)數(shù)?? ?private String operator;//運(yùn)算符?? ?private String result;//運(yùn)算結(jié)果?? ??? ?//定義set和get方法?? ?public String getFirst() {?? ??? ?return first;?? ?}?? ?public void setFirst(String first) {?? ??? ?this.first = first;?? ?}?? ?public String getSecond() {?? ??? ?return second;?? ?}?? ?public void setSecond(String second) {?? ??? ?this.second = second;?? ?}?? ?public String getOperator() {?? ??? ?return operator;?? ?}?? ?public void setOperator(String operator) {?? ??? ?this.operator = operator;?? ?}?? ?public String getResult() {?? ??? ?return result;?? ?}?? ?public void setResult(String result) {?? ??? ?this.result = result;?? ?}?? ??? ?}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持。
標(biāo)簽:
JSP
相關(guān)文章:
1. jsp+servlet簡單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))2. 淺談SpringMVC jsp前臺獲取參數(shù)的方式 EL表達(dá)式3. JSP靜態(tài)導(dǎo)入與動(dòng)態(tài)導(dǎo)入使用詳解4. 詳解JSP 內(nèi)置對象request常見用法5. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)財(cái)務(wù)記賬管理系統(tǒng)6. jsp網(wǎng)頁實(shí)現(xiàn)貪吃蛇小游戲7. jsp+servlet實(shí)現(xiàn)猜數(shù)字游戲8. 如何在jsp界面中插入圖片9. Jsp中request的3個(gè)基礎(chǔ)實(shí)踐10. jsp實(shí)現(xiàn)textarea中的文字保存換行空格存到數(shù)據(jù)庫的方法
排行榜
