水仙数问题的常见困难
您提供的求水仙数的 java 代码中存在以下问题:
判断错误
代码中将判断语句 is 放置在 is() 方法中,导致在判断过程中重新调用 reader.nextint(),阻塞了程序。这样代码中永远不能继续执行。
修改后代码
您提供的代码,经修改后如下:
public class c1t3 { public static Integer shuru() { Scanner reader = new Scanner(System.in); System.out.println("123"); int date = reader.nextInt(); return date; } public static void main(String[] args){ System.out.println("请输入一个三位数的整数"); String a= shuru().toString(); String[]b=a.split(""); int[] math=new int[b.length]; for(int i=0;i<b.length; i++) { math[i] = Integer.parseInt(b[i]); } if (is(math[0], math[1], math[2], Integer.parseInt(a))) { System.out.println("这个数是水仙数"); } else { System.out.println("这个不是水仙数"); } } public static boolean is(int a,int b,int c, int num){ return(a*a*a)+(b*b*b)+(c*c*c)== num; } }
修改内容
以上就是水仙数判断问题:为什么代码会陷入死循环?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号