JSP實現簡單網頁計算器
本文實例為大家分享了jsp實現簡單網頁計算器的具體代碼,供大家參考,具體內容如下
一、構造一個簡單的計算器,能夠進行“+、—、*、/”運算
(1)編寫jsp頁面,用戶通過表單輸入兩個操作數和運算符,調用該頁面自身處理該表單,通過調用simplecalculator類的實例實現運算邏輯,并顯示運算結果。
(2)實現下邊的jsp網頁計算器:
二、代碼實現
(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"> ? ? ? ??? ??? ?第一個數:<input type="text" value="" name="first" size="25px"/> ? ? ? ??? ??? ?<br /><br /> ? ? ? ??? ??? ?第二個數:<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 /> ? ? ? ??? ??? ?<% ? ? ? ??? ??? ? ? ? ? ??? ??? ??? ??? ??? ?//獲取表單中的數據進行運算 ?? ??? ??? ??? ??? ??? ?string first = request.getparameter("first");//第一個數 ?? ??? ??? ??? ??? ??? ?string second = request.getparameter("second");//第二個數 ?? ??? ??? ??? ??? ??? ?string operator = request.getparameter("operator");//運算符 ?? ??? ??? ??? ? ?? ??? ?string result = "" ;//運算結果 ?? ??? ??? ??? ? ?? ??? ? ? ? ? ??? ??? ??? ??? ??? ?//判斷運算符 ? ? ? ??? ??? ??? ??? ??? ?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 = "除數不能為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("計算結果:"+first+operator+second+" = "+simplecalculator.getresult()); ? ? ? ??? ??? ??? ??? ??? ??? ?out.print("<h4>"); ? ? ? ??? ??? ??? ??? ??? ?}else{ ? ? ? ??? ??? ??? ??? ??? ??? ?out.print("計算錯誤");? ? ? ? ??? ??? ??? ??? ??? ?} ? ? ? ??? ??? ? ? ? ? ??? ??? ??? ??? ?%> ? ? ? ??? ??? ??? ??? ? ?<br /><br /> </body> </html>
(2)simplecalculator類
public class simplecalculator { ?? ? ?? ?//定義變量 ?? ?private string first;//第一個數 ?? ?private string second;//第二個數 ?? ?private string operator;//運算符 ?? ?private string result;//運算結果 ?? ? ?? ?//定義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; ?? ?} ?? ? ?? ? }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持碩編程。