Java 如何检查一个整数是否是一个完全平方数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34056609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:16:29  来源:igfitidea点击:

How to check if an integer is a perfect square

javadrjavaperfect-square

提问by Brooke

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!

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

采纳答案by Jaskaranbir Singh

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