求: 编写一个JavaScript程序,根据输入的正整数,计算对应的平方、平方根。

JavaScript06

求: 编写一个JavaScript程序,根据输入的正整数,计算对应的平方、平方根。,第1张

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>

<%

String path = request.getContextPath()

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<base href="<%=basePath%>">

<title>My JSP 'index.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">

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

<script type="text/javascript">

function getById(id){

return document.getElementById(id)

}

function getInfo(){

var num=getById("num").value

if(isNaN(num) || num<1){

alert("请输入正整数!")

}else{

alert(num+"的平方值为:"+num*num)

alert(num+"的平方根为:"+Math.sqrt(num))

}

}

</script>

</head>

<body>

<input type="text" size="20" id="num"><input type="button" value="计算" onclick="getInfo()">

</body>

</html>

楼主,望采纳啊,^_^,给点评价哦 ,可以结贴了。绝对没问题

<body>

            <input type="text" name="" id="aa" value="" />        

            <input type="button" name="btn" id="btn" value="平方值" />

            <p></p>

</body>

<script type="text/javascript">

    var aa=document.getElementById('aa')   

    var btn=document.getElementById('btn')

    var p=document.getElementsByTagName('p')[0]

    btn.onclick=function(){

     if(isNaN(aa.value)){

     alert("您输入的不是数字")

      }else{

       a=aa.value*aa.value

     p.innerHTML=a

      }

    }     

</script>

*/

package questions

public class Q10

{

public static void main(String[]args)

{

int n=0

for(int i=0i<=100000i++)//先确定一个在100000内的大概范围

{

if(isCompSqrt(i+100))

{

n=i

break

}

}

System.out.print("所求的数是:"+n)

}

private static boolean isCompSqrt(int p)//判断完全平方数的方法

{

boolean flag=false

double fsqrt=Math.sqrt(p)//先将数开平方

int q=(int)fsqrt//转换成整数,另q为开平方且转换为整数的结果。

if(p==Math.pow(q,2))//pow(x,y)就是计算x的y次幂。把开平方后的整数再平方,看看它和开平方之前的数是不是相等。

{

flag=true

}

return flag

}

}