用JAVA实现两数相加!
JAVA课作业,要求把下面的代码以exam5-1.jsp为名字转换实行publicclassMyClass{intnum1=20;intnum2=50;publicint...
JAVA课作业,要求把下面的代码以exam5-1.jsp为名字转换实行
public class MyClass {
int num1 =20;
int num2 =50;
public int sum() {
return num1 + num2;
}
}
要求结果可以在画面上确定
实现结果:<%=sum() %> 展开
public class MyClass {
int num1 =20;
int num2 =50;
public int sum() {
return num1 + num2;
}
}
要求结果可以在画面上确定
实现结果:<%=sum() %> 展开
1个回答
2013-05-27
展开全部
要求不是很清楚。你是需要一个页面供用户输入两个数,点击一个按钮,然后显示计算结果是吧?一定要用<%=sum() %>吗?
以下代码复制、粘贴,保存为html或jsp均可
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'MyJsp.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">
<script type="text/javascript">
function sum(){
var num1 = document.getElementById("num1");
var num2 = document.getElementById("num2");
var result = document.getElementById("result");
result.innerHTML = num1.value * 1 + num2.value * 1;
if(result.innerHTML == "NaN"){
result.innerHTML = "计算结果不是数字";
}
}
</script>
</head>
<body>
<input type="text" id="num1" size="8" />
+
<input id="num2" size="8" />
=
<span id="result"></span>
<br />
<input type="button" value="计算" onclick="sum()" />
</body>
</html>
以下代码复制、粘贴,保存为html或jsp均可
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'MyJsp.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">
<script type="text/javascript">
function sum(){
var num1 = document.getElementById("num1");
var num2 = document.getElementById("num2");
var result = document.getElementById("result");
result.innerHTML = num1.value * 1 + num2.value * 1;
if(result.innerHTML == "NaN"){
result.innerHTML = "计算结果不是数字";
}
}
</script>
</head>
<body>
<input type="text" id="num1" size="8" />
+
<input id="num2" size="8" />
=
<span id="result"></span>
<br />
<input type="button" value="计算" onclick="sum()" />
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |