用java输出所有囫囵数并统计个数
时间: 2024-05-15 08:15:00 浏览: 9
囫囵数又称为完全立方数,是指一个数可以表示为一个整数的立方。例如,1、8、27、64等都是囫囵数。
以下是使用Java语言输出所有囫囵数并统计个数的代码:
```java
public class PerfectCube {
public static void main(String[] args) {
int count = 0;
for (int i = 1; i <= 100; i++) {
int cubeRoot = (int)Math.cbrt(i);
if (cubeRoot * cubeRoot * cubeRoot == i) {
System.out.println(i);
count++;
}
}
System.out.println("共有" + count + "个囫囵数");
}
}
```
代码中使用了一个for循环遍历1到100之间的所有数,然后通过Math类的cbrt方法计算每个数的立方根,判断是否为整数立方,如果是则输出该数并统计个数。最后输出所有囫囵数的个数。
运行结果为:
```
1
8
27
64
共有4个囫囵数
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)