首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

解答!

2012-04-03 
在线等解答!!!!!!!!!!!!!!!!MathBean.java___________________________________________________packagete

在线等解答!!!!!!!!!!!!!!!!
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
 这是怎么起的?  然后解决呢? 谢谢各位!


[解决办法]
lz是新手吧~~
你这个程序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了!

热点排行