【问题标题】:Perfect numbers method terminating?完美数字方法终止?
【发布时间】:2014-11-10 20:22:55
【问题描述】:

我正在编写一个方法,用户的输入将输出相应的完美数字。例如,如果用户的输入是“6”,那么输出就是“2016”。现在,如果您尝试输入一个数字,它就会终止。谢谢。

    public static void perfectNumber(){
                System.out.println("Please input a number.");
                Scanner inputPN = new Scanner(System.in);
                int r = inputPN.nextInt();
                for (int n = 1; n > r; n++) {  
                    int spd = 1;
                    for (int d = 2; d <= n/2; d++) {
                        if (spd==n) System.out.print(n); 
                        }

【问题讨论】:

  • 您的实际问题/疑问是什么?从你的描述中我不清楚。
  • 还有,,给出完整的方法。

标签: java perfect-numbers


【解决方案1】:
for (int n = 1; n > r; n++)

这里你说,只要 n 大于 r,for 循环就应该运行。

所以它只有在你将 0 或更低的值设置为 nextInt() 时才会运行,在所有其他情况下它都不会运行。

【讨论】:

    猜你喜欢
    • 2018-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-05
    • 2010-12-05
    • 1970-01-01
    相关资源
    最近更新 更多