import java.io.*;
public class CalMeasure {
int weight;
int age;
int result =0;//1
String gender;
public CalMeasure(){
}
public int getWeight(){
return this.weight;
}
public int getAge(){
return this.age;
}
public String getGender(){
return this.gender;
}
public int getResult(){
return calculate();//
}
public void setWeight(int weight_){
this.weight = weight_;
}
public void setAge(int age_){
this.age = age_;
}
public void setGender(String gender_){
this.gender = gender_;
}
private int calculate(){
if(this.result!=0){
return this.result;
}else{
//float fltTEN = (float)(weight * 2); //2
this.result = 2;//(int)fltTEN;
return this.result;//////////////
}//else
}
public static void main(String[] args) {//静态
CalMeasure p=new CalMeasure(); ////
System.out.println(p.result); //// echo 0??
}
}//class
为什么输出的是0而不是2??? 谢谢大家指教
----------------解决方案--------------------------------------------------------
因为1:int result =0;你定义了result=0;
2: System.out.println(p.result)只是打印result.
3: public int getResult(){
return calculate();
}
此方法没被执行。
想打印2,用System.out.println(p.getResult())好像就可以了~~
----------------解决方案--------------------------------------------------------
当然是0,你又没有调用calculate()方法,谁给它赋了2 了?
----------------解决方案--------------------------------------------------------
好了,谢谢2位
----------------解决方案--------------------------------------------------------
还是这个类,现在有2个JSP页面调用这个类,可怎么有错误呢?
input.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<body>
<table width="560" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<form name="form1" method="post" action="input2.jsp">
<tr>
<td height="42" colspan="2" valign="top">测试:</td>
</tr>
<tr>
<td width="121" height="26" align="right" valign="top">性别:</td>
<td width="439" valign="top">
<input type="radio" name="gender" value="male">
男
<input type="radio" name="gender" value="female">
女
</td>
</tr>
<tr>
<td height="24" align="right" valign="top">年龄:</td>
<td valign="top"><input type="text" name="age"></td>
</tr>
<tr>
<td height="24" align="right" valign="top">体重:</td>
<td valign="top"><input type="text" name="weight"></td>
</tr>
<tr>
<td height="27" valign="top"><input type="submit" name="Submit" value="提交"></td>
<td valign="top"><!--DWLayoutEmptyCell--> </td>
</tr>
<tr>
<td height="158"> </td>
<td> </td>
</tr></form>
</table>
</body>
</html>
input2.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean id = "Calculator" class ="CalMeasure" scope="session" />
<jsp:setProperty name ="Calculator" property="*" />
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<body>
你的结果:<jsp:getProperty name="Calculator" property="result" />
</body>
</html>
CalMeasure.class
import java.io.*;
public class CalMeasure {
int weight;
int age;
int result =0;//1
String gender;
public CalMeasure(){
}
public int getWeight(){
return this.weight;
}
public int getAge(){
return this.age;
}
public String getGender(){
return this.gender;
}
public int getResult(){
return calculate();//
}
public void setWeight(int weight_){
this.weight = weight_;
}
public void setAge(int age_){
this.age = age_;
}
public void setGender(String gender_){
this.gender = gender_;
}
private int calculate(){
if(this.result!=0){
return this.result;
}else{
float fltTEN = (float)(weight * 2); //2
this.result = (int)fltTEN;//2;
return this.result;//////////////
}//else
}
public static void main(String[] args) {
CalMeasure p=new CalMeasure();
System.out.println(p.calculate());
}
}//class
出现错误:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: /input2.jsp(3,0) The value for the useBean class attribute CalMeasure is invalid.
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:39)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:409)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:150)
org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1227)
org.apache.jasper.compiler.Node$UseBean.accept(Node.java:1116)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2163)
org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2213)
org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2219)
org.apache.jasper.compiler.Node$Root.accept(Node.java:456)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2163)
org.apache.jasper.compiler.Generator.generate(Generator.java:3272)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:244)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:470)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:451)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:439)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:511)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:295)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.0.28 logs.
----------------解决方案--------------------------------------------------------