MathBean.java
___________________________________________________
package test;public class MathBean {
private String a;
private String b;
private double result;
private String operator;
public String getA() {
if(a==null)
a="";
return a;
}
public void setA(String a) {this.a = a;
}
public String getB() {
if(b==null)
b="";
return b;
}
public void setB(String b) {
this.b = b;
}
public String getOperator() {
return operator;
}
public void setOperator(String operator) {
this.operator = operator;
}
public double getResult() {
return result;
}
public void forResult() {
double one=Double.parseDouble(a);
double two=Double.parseDouble(b);
try {
if (this.equals("+")) result=one+two;
else if (this.equals("-")) result=one-two;
else if (this.equals("*")) result=one*two;
else if (this.equals("/")) result=one/two;
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
}
}
___forRrsult.JSP________________________________________
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<jsp:useBean id="forResult"class="test.MathBean"scope="request">
<jsp:setProperty name="forResult" property="*"/>
</jsp:useBean>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312"/>
<title> 一简单的计数器</title>
</head>
<body><center>
<form action="forResult.jsp" method="POST">
<table border="1" width="300"bgcolor="#ffffc0">
<tr><td>计算结果</td>
<%
try{forResult.forResult();}
catch(Exception e){e.printStackTrace();}%>
<td><%=forResult.getA()+forResult.getOperator()+forResult.getB()%>=
<%=forResult.getResult()%></td></tr>
<tr><td>第一个数</td>
<td><input type="text"name="a"></td></tr>
<tr><td>选择操作</td>
<td><SELECT name="operator">
<OPTION value="+">+</OPTION>
<OPTION value="-">-</OPTION>
<OPTION value="*">*</OPTION>
<OPTION value="/">/</OPTION>
</SELECT></td>
</tr>
<TR><TD>第二个数</TD>
<td><INPUT type="text"name="b"></td></TR>
<TR><TD><INPUT type="submit"value="确定"></TD>
<td><INPUT type="reset"value="取消"></td></TR>
</table>
</form>
</center>
This is my JSP page. <br/>
</body>
</html>
---------------------------------------------------------------
能把值传入 但是我却的不到结果 比如输入3*2 可结果依然是0.0
这是怎么起的? 然后解决呢? 谢谢各位!
___________________________________________________
package test;public class MathBean {
private String a;
private String b;
private double result;
private String operator;
public String getA() {
if(a==null)
a="";
return a;
}
public void setA(String a) {this.a = a;
}
public String getB() {
if(b==null)
b="";
return b;
}
public void setB(String b) {
this.b = b;
}
public String getOperator() {
return operator;
}
public void setOperator(String operator) {
this.operator = operator;
}
public double getResult() {
return result;
}
public void forResult() {
double one=Double.parseDouble(a);
double two=Double.parseDouble(b);
try {
if (this.equals("+")) result=one+two;
else if (this.equals("-")) result=one-two;
else if (this.equals("*")) result=one*two;
else if (this.equals("/")) result=one/two;
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
}
}
___forRrsult.JSP________________________________________
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<jsp:useBean id="forResult"class="test.MathBean"scope="request">
<jsp:setProperty name="forResult" property="*"/>
</jsp:useBean>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312"/>
<title> 一简单的计数器</title>
</head>
<body><center>
<form action="forResult.jsp" method="POST">
<table border="1" width="300"bgcolor="#ffffc0">
<tr><td>计算结果</td>
<%
try{forResult.forResult();}
catch(Exception e){e.printStackTrace();}%>
<td><%=forResult.getA()+forResult.getOperator()+forResult.getB()%>=
<%=forResult.getResult()%></td></tr>
<tr><td>第一个数</td>
<td><input type="text"name="a"></td></tr>
<tr><td>选择操作</td>
<td><SELECT name="operator">
<OPTION value="+">+</OPTION>
<OPTION value="-">-</OPTION>
<OPTION value="*">*</OPTION>
<OPTION value="/">/</OPTION>
</SELECT></td>
</tr>
<TR><TD>第二个数</TD>
<td><INPUT type="text"name="b"></td></TR>
<TR><TD><INPUT type="submit"value="确定"></TD>
<td><INPUT type="reset"value="取消"></td></TR>
</table>
</form>
</center>
This is my JSP page. <br/>
</body>
</html>
---------------------------------------------------------------
能把值传入 但是我却的不到结果 比如输入3*2 可结果依然是0.0
这是怎么起的? 然后解决呢? 谢谢各位!
解决方案 »
- juery tree树形结构遍历问题,求大神解决。方便的留个地址
- 菜鸟提问 application session request cookie 生命周期
- 一个项目
- 关于Struts中java bean的问题
- web 工程 关于起服务的时候出现的spring配置action初始化失败的问题
- 请教一个关于SQL SERVER JDBC的问题
- <求助>请问各位大侠:用window.showModalDialog弹出一页面;怎样实现修改数据后,父页面自动刷新???
- IKAnalyzer使用API扩展字典,不停服务情况下如何删除、修改的字典
- 我到tomcat官方网下载了一个tomcat1.4,可它怎么不支持中文的?
- jsp 链接weblogic的链接池问题
- 100
- 怎样在数据库中保存iframe的内容?
这里的这个计算器很完善,稍微该下就可以了:
http://www.jb51.net/html/200702/44/7054.htm
你这个程序2个地方错误~
1,你页面上的值没有传给bean:该法如下
<td>计算结果</td>
<%
try {
//新加代码,给bean传值
String a = request.getParameter("a");
String b = request.getParameter("b");
String operator = request.getParameter("operator");
forResult.setA(a);
forResult.setB(b);
forResult.setOperator(operator);
//以上新加代码
forResult.forResult();
} catch (Exception e) {
e.printStackTrace();
}
%>
<td>2,bean里面有一处错误,估计是你手误:
try {
if (this.equals("+"))
result = one + two;
else if (this.equals("-"))
result = one - two;
else if (this.equals("*"))
result = one * two;
else if (this.equals("/"))
result = one / two;
}
上面代码里的所有this都改成operator
try {
if (operator.equals("+"))
result = one + two;
else if (operator.equals("-"))
result = one - two;
else if (operator.equals("*"))
result = one * two;
else if (operator.equals("/"))
result = one / two;
}OK了!