
水仙数代码问题分析
如题所示,求水仙数的 java 代码出现了控制台无反应的问题。下面分析代码中可能出现的问题:
主方法的 shuru() 方法阻塞了代码执行。在 is() 方法中,将 shuru() 作为判断条件,导致代码在判断过程中再次阻塞,等待控制台输入。
正确的写法是:
立即学习“Java免费学习笔记(深入)”;
public static boolean is(int a, int b, int c, int num) {
return (a * a * a) + (b * b * b) + (c * c * c) == num;
}在 is() 方法中直接判断三个数字的立方和是否等于原数,而无需再次调用 shuru() 方法。
修改后的代码如下:
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;
}
}以上就是Java 水仙数代码控制台无反应,问题出在哪?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号