JSP實現(xiàn)簡單網(wǎng)頁計算器
本文實例為大家分享了JSP實現(xiàn)簡單網(wǎng)頁計算器的具體代碼,供大家參考,具體內(nèi)容如下
一、構(gòu)造一個簡單的計算器,能夠進行“+、—、*、/”運算
(1)編寫jsp頁面,用戶通過表單輸入兩個操作數(shù)和運算符,調(diào)用該頁面自身處理該表單,通過調(diào)用SimpleCalculator類的實例實現(xiàn)運算邏輯,并顯示運算結(jié)果。
(2)實現(xiàn)下邊的jsp網(wǎng)頁計算器:
二、代碼實現(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>計算器</title> </head> <body> ? ? ? ? ? ? ? ? ? ?<form action="" method="post"> ? ? ? ??? ??? ?第一個數(shù):<input type="text" value="" name="first" size="25px"/> ? ? ? ??? ??? ?<br /><br /> ? ? ? ??? ??? ?第二個數(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ù)進行運算 ?? ??? ??? ??? ??? ??? ?String first = request.getParameter("first");//第一個數(shù) ?? ??? ??? ??? ??? ??? ?String second = request.getParameter("second");//第二個數(shù) ?? ??? ??? ??? ??? ??? ?String operator = request.getParameter("operator");//運算符 ?? ??? ??? ??? ? ?? ??? ?String result = "" ;//運算結(jié)果 ?? ??? ??? ??? ? ?? ??? ? ? ? ? ??? ??? ??? ??? ??? ?//判斷運算符 ? ? ? ??? ??? ??? ??? ??? ?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) )); ? ? ? ??? ??? ??? ??? ??? ??? ?} ? ? ? ??? ??? ??? ??? ??? ?} ? ? ? ??? ??? ??? ??? ??? ? ? ? ? ??? ??? ??? ??? ??? ?//定義一個計算器類 ? ? ? ??? ??? ??? ??? ??? ?SimpleCalculator simpleCalculator = new SimpleCalculator(); ? ? ? ??? ??? ??? ??? ??? ?simpleCalculator.setResult(result); ? ? ? ??? ??? ??? ??? ??? ?if( !simpleCalculator.getResult().equals("") && simpleCalculator.getResult() != null){ ? ? ? ??? ??? ??? ??? ??? ??? ?out.print("<h2 style= 'color: blue'>"); ? ? ? ??? ??? ??? ??? ??? ??? ?out.print("計算結(jié)果:"+first+operator+second+" = "+simpleCalculator.getResult()); ? ? ? ??? ??? ??? ??? ??? ??? ?out.print("<h4>"); ? ? ? ??? ??? ??? ??? ??? ?}else{ ? ? ? ??? ??? ??? ??? ??? ??? ?out.print("計算錯誤");? ? ? ? ??? ??? ??? ??? ??? ?} ? ? ? ??? ??? ? ? ? ? ??? ??? ??? ??? ?%> ? ? ? ??? ??? ??? ??? ? ?<br /><br /> </body> </html>
(2)SimpleCalculator類
public class SimpleCalculator { ?? ? ?? ?//定義變量 ?? ?private String first;//第一個數(shù) ?? ?private String second;//第二個數(shù) ?? ?private String operator;//運算符 ?? ?private String result;//運算結(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í)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JSP 開發(fā)中過濾器filter設(shè)置編碼格式的實現(xiàn)方法
這篇文章主要介紹了JSP 開發(fā)中過濾器filter設(shè)置編碼格式的實現(xiàn)方法的相關(guān)資料,我們知道為了避免提交數(shù)據(jù)的亂碼問題,需要在每次使用請求之前設(shè)置編碼格式,這里提供一次性修改所有的請求編碼問題,需要的朋友可以參考下2017-08-08JSP+Servlet實現(xiàn)文件上傳到服務(wù)器功能
這篇文章主要為大家詳細(xì)介紹了JSP+Servlet實現(xiàn)文件上傳到服務(wù)器功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-09-09JSP組件commons-fileupload實現(xiàn)文件上傳
這篇文章主要為大家詳細(xì)介紹了JSP組件commons-fileupload實現(xiàn)文件上傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10Jsp和PHP共用80端口整合Apache和Tomcat(訪問時無需加端口號)
整合Apache和Tomcat,使得Java工程和PHP工程都能共用80端口,訪問網(wǎng)站時,無需在地址欄中加端口號,具體實現(xiàn)如下,感興趣的朋友可以參考下哈2013-06-06基于jsp+mysql實現(xiàn)在線水果銷售商城系統(tǒng)
這篇文章主要介紹了全新基于jsp+mysql實現(xiàn)的一個在線水果銷售商城系統(tǒng),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08