国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

JSP實現計算器功能(網頁版)

2024-09-05 00:22:27
字體:
來源:轉載
供稿:網友
這篇文章講述了JSP實現計算器功能的詳細代碼,網頁版的計算器具有一定的參考價值,感興趣的小伙伴們可以參考一下
 

jsp實現網頁計算器代碼如下:只有兩個jsp頁面
myCal.jsp如下:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>">  <title>My JSP 'myCal.jsp' starting page</title>  <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page">   <!-- jsp頁面中不可以直接使用script --> <script language="javascript"> <!--     //寫一個函數判斷是否兩個數都有     function checkNum()     {       //判斷num1 num2是否為空       if((form1.num1.value == "") || (form1.num2.value == ""))       {         window.alert("null,不能為空!");         return false;       }       //判斷是否是數字       if(Math.round(form1.num1.value) != form1.num1.value  && Math.round(form1.num2.value) != form1.num2.value)       {         window.alert("num1和num2不是一個數");         return false;       }       if(Math.round(form1.num1.value) != form1.num1.value)       {         window.alert("num1不是一個數");         return false;       }       if(Math.round(form1.num2.value) != form1.num2.value)       {         window.alert("num2不是一個數");         return false;       }            }      -->   </script> </head> <body>    <form name="form1" action="calculator/myResult.jsp" method="post">     請輸入第一個數:<input type="text" name="num1">      <select name="flag">       <option value=+>+</option>       <option value=->-</option>       <option value=*>*</option>       <option value=/>/</option>     </select>      請輸入第二個數:<input type="text" name="num2">      <input type="submit" value="計算" onclick="return checkNum();">   </form>  </body> </html> 

myResult.jsp如下:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html>  <head>   <base href="<%=basePath%>">      <title>My JSP 'myResult.jsp' starting page</title>      <meta http-equiv="pragma" content="no-cache">   <meta http-equiv="cache-control" content="no-cache">   <meta http-equiv="expires" content="0">     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">   <meta http-equiv="description" content="This is my page">   <!--   <link rel="stylesheet" type="text/css" href="styles.css">   -->   </head>    <body>  <%   //第1步接收到 第1個數   String s_num1 = request.getParameter("num1");   //第2步接收到 第2個數   String s_num2 = request.getParameter("num2");   //第3步接收到 運算符   String flag = request.getParameter("flag");   //第4步 計算      int num1 = Integer.parseInt(s_num1);   int num2 = Integer.parseInt(s_num2);   int result = 0;   if(flag.equals("+"))   {     result = num1+num2;   }   else if(flag.equals("-"))   {     result = num1-num2;   }   else if(flag.equals("*"))   {     result = num1*num2;   }   else if(flag.equals("/"))   {     result = num1/num2;   }   //第5步   out.println("結果是:"+result);   %>      </body> </html>

雖然過程很簡單但是有幾個值得學習的地方:
如何判斷輸入的數據是不是數字:使用Math.round(form1.num1.value) != form1.num1.value   來判斷;
如何獲取操作值:設置name屬性 flag實現。

希望本文所述對大家學習JSP編程有所幫助。



注:相關教程知識閱讀請移步到JSP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宁国市| 交城县| 浮山县| 绩溪县| 奎屯市| 临泉县| 屏南县| 拜泉县| 天镇县| 红桥区| 汨罗市| 安阳市| 浦东新区| 阿拉尔市| 七台河市| 长宁县| 长春市| 北海市| 西昌市| 太白县| 石景山区| 吉林省| 西青区| 宾川县| 长顺县| 浠水县| 沙雅县| 兴文县| 平阳县| 旌德县| 蒙自县| 汤阴县| 安宁市| 内丘县| 乌恰县| 廊坊市| 珲春市| 洪湖市| 福鼎市| 滁州市| 馆陶县|