文章詳情頁(yè)
JSP實(shí)現(xiàn)簡(jiǎn)單網(wǎng)頁(yè)計(jì)算器
瀏覽:51日期:2022-06-07 17:54:09
本文實(shí)例為大家分享了JSP實(shí)現(xiàn)簡(jiǎn)單網(wǎng)頁(yè)計(jì)算器的具體代碼,供大家參考,具體內(nèi)容如下
一、構(gòu)造一個(gè)簡(jiǎn)單的計(jì)算器,能夠進(jìn)行“+、—、*、/”運(yùn)算
(1)編寫jsp頁(yè)面,用戶通過表單輸入兩個(gè)操作數(shù)和運(yùn)算符,調(diào)用該頁(yè)面自身處理該表單,通過調(diào)用SimpleCalculator類的實(shí)例實(shí)現(xiàn)運(yùn)算邏輯,并顯示運(yùn)算結(jié)果。
(2)實(shí)現(xiàn)下邊的jsp網(wǎng)頁(yè)計(jì)算器:
二、代碼實(shí)現(xiàn)
(1)jsp頁(yè)面
<%@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;?? ?}?? ??? ?}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持。
標(biāo)簽:
JSP
相關(guān)文章:
1. jsp實(shí)現(xiàn)textarea中的文字保存換行空格存到數(shù)據(jù)庫(kù)的方法2. jsp cookie+session實(shí)現(xiàn)簡(jiǎn)易自動(dòng)登錄3. 詳解JSP 內(nèi)置對(duì)象request常見用法4. Jsp中request的3個(gè)基礎(chǔ)實(shí)踐5. javascript實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能6. jsp+dao+bean+servlet(MVC模式)實(shí)現(xiàn)簡(jiǎn)單用戶登錄和注冊(cè)頁(yè)面7. SSM框架整合JSP中集成easyui前端ui項(xiàng)目開發(fā)示例詳解8. jsp filter 過濾器功能與簡(jiǎn)單用法示例9. JSP出現(xiàn)中文亂碼問題解決方法詳解10. JSP實(shí)現(xiàn)客戶信息管理系統(tǒng)
排行榜
