标题:为什么输出的是0而不是2???
只看楼主
hunkzhang
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2007-1-16
 问题点数:0 回复次数:4 
为什么输出的是0而不是2???

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??? 谢谢大家指教

搜索更多相关主题的帖子: public return import result 
2007-01-16 14:22
支离破碎
Rank: 6Rank: 6
等 级:贵宾
威 望:23
帖 子:737
专家分:0
注 册:2007-1-4
得分:0 
因为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())好像就可以了~~

人生漂泊無依,有如浮萍菱花,隨水飄流,你会在我这里停留吗?
[url=http://51mynet.]http://51mynet.[/url]
2007-01-16 14:26
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
得分:0 
当然是0,你又没有调用calculate()方法,谁给它赋了2 了?

可惜不是你,陪我到最后
2007-01-16 14:27
hunkzhang
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2007-1-16
得分:0 
好了,谢谢2位
2007-01-16 14:37
hunkzhang
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2007-1-16
得分:0 

还是这个类,现在有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-->&nbsp;</td>
</tr>
<tr>
<td height="158">&nbsp;</td>
<td>&nbsp;</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.


2007-01-16 14:45



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-116282-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.357104 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved