如何检查一个整数是否是一个完美的正方形 [英] How to check if an integer is a perfect square

查看:94
本文介绍了如何检查一个整数是否是一个完美的正方形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何编写if-then语句来检查输入的整数是否是理想的平方(即,如果我取平方根,那么它也将是整数:4、9、16、25、36等)在DrJava中?谢谢!

How could I write an if-then statement that checks if an inputted integer is a perfect square or not (i.e. if I took the square root, it would be an integer as well: 4, 9, 16, 25, 36, etc.) in DrJava? Thank you!

推荐答案

我知道这个问题已经有了答案....但是以防万一,这也可行.

I am aware that this question already has an answer.... But just in case, this also works.

int x = (int) Math.sqrt(input);
if(Math.pow(x,2) == input)
    //Do stuff

这篇关于如何检查一个整数是否是一个完美的正方形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆