【问题标题】:List of divisors of a number一个数的除数列表
【发布时间】:2026-02-13 00:20:04
【问题描述】:

这是我的第一篇文章,所以如果我写了一些愚蠢的东西踩我。

我刚开始上 IT 课,今天在“while”循环课上,我的导师给了我们以下作业:

编写一个程序,它读取一个自然数 n,并在一个图形框中显示它在区间 [2; 中的所有除数; n-1]。

到目前为止,我想出了一个有效的代码,但结果有点错误:

import java.util.Arrays;
import javax.swing.JOptionPane;

public class Divisors {
    public static void main(String[] args) {
        String n = JOptionPane.showInputDialog(null, "Enter a natural number");
        Integer i = Integer.parseInt(n);

        int d = i - 1;
        int x = 2;
        int[] dvr = new int[i]; // [i] because bigger numbers need more iterations

        while (x >= 2 && x <= d) {
            double y = i % x;

            if (y == 0) {
                dvr[x] = x;
                x = x + 1;
            } else {
                x = x + 1;
            }
        }

        JOptionPane.showMessageDialog(null, "The divisors of " + i + " are:\n" + Arrays.toString(dvr));
    }
}

问题是循环用很多零填充数组,并且导师结果的屏幕截图显示了一个仅列出除数的窗口。

我尝试使用 ArrayList 来做到这一点,但这对我来说现在是黑魔法,而且我的导师还没有教我们如何使用我代码中使用的东西之外的任何东西。

非常感谢任何帮助。

【问题讨论】:

  • 实际上,数组在创建时被初始化为全零,您的循环不会用零填充它。
  • 你的问题在于 dvr[x] = x;您不想在位置 x 处设置除数的值。您需要第二个变量来索引到您的数组,您只在添加每个除数后递增。
  • @bhspencer 但我在数组中仍有预定数量的插槽
  • @azurefrog 抱歉,我知道,打错了。但是有没有办法只显示除数?除了将它们保存在数组中并保存到 showMessageDialog 中之外,我想不出任何更好的方法...
  • it's my first post so DO stomp me if I wrote something stupid. 实际上,看到一个包含mvce 和具体问题描述的问题令人耳目一新。

标签: java arrays while-loop joptionpane


【解决方案1】:

您遇到的主要问题是要打印的值数量未知,但您使用数组来存储它们,并且数组具有固定大小。由于您有一个 int 数组,因此它将完全使用默认值零填充。

理想情况下,您只打印数组的第一组非零值,但您存储的是分散在整个数组中的除数。

dvr[x] = x; 将每个值存储在该值的索引处,而实际上您应该将每个新值存储到数组中的下一个开放位置。

创建一个单独的索引变量,并使用它来存储每个值:

    int index = 0;
    while (x >= 2 && x <= d) {
    ...
        if (y == 0) {
            dvr[index++] = x;
    ...

然后当你的主循环完成后,你可以创建一个新的“显示数组”,它只包含除数,而不是零。此时,index 会准确告诉您它需要多大:

    int[] display = Arrays.copyOf(dvr, index);
    JOptionPane.showMessageDialog(null, "The divisors of " + i + " are:\n" + Arrays.toString(display));

【讨论】:

  • 非常感谢!这是一个完美的解释。
【解决方案2】:

在 Java 中,int 的默认值为零。所以这就是为什么你会看到很多零。

由于您将数组的大小定义为 i,这比所需的要大,因为除数的数量总是小于 i

因此,您不应打印整个数组,而应仅将其打印到除数总数,您应该为其单独变量而不是使用x

这是修改后的版本,我使用单独的index 变量来跟踪从0 开始的除数数。最后,您可以将数组打印到index

import java.util.Arrays;
import javax.swing.JOptionPane;

public class Divisors {
public static void main(String[] args) {
    String n = JOptionPane.showInputDialog(null, "Enter a natural number");
    Integer i = Integer.parseInt(n);

    int d = i - 1;
    int index = 0;
    int x=2;
    int[] dvr = new int[i]; // [i] because bigger numbers need more iterations

    while (x >= 2 && x <= d) {
        double y = i % x;

        if (y == 0) {
            dvr[index] = x;
            x = x + 1;
            index= index + 1;
        } else {
            x = x + 1;
        }
    }

    JOptionPane.showMessageDialog(null, "The divisors of " + i + " are:\n" + Arrays.copyOfRange(drv, 0, index));
}
}

【讨论】:

  • 问题是,如果除数条件检查失败,x 变量也会增加(参见其他部分)。所以 x,实际上并不代表除数的数量。
  • 是的,问题已解决。
  • 应该说:int 的默认值为零。
【解决方案3】:

Set 数据结构避免重复,您可以使用它来克服重复除数被添加到数据结构中的问题。

    import java.util.*;
    import javax.swing.JOptionPane;

    public class Divisors {
        public static void main(String[] args) {
            String n = JOptionPane.showInputDialog(null, "Enter a natural number");
            Integer i = Integer.parseInt(n);

            int d = i - 1;
            int x = 2;
            Set<Integer> divisors = new HashSet<>();

            while (x >= 2 && x <= d) {
                double y = i % x;

                if (y == 0) {
                     divisors.add(x);
                     x = x + 1;
                } else {
                     x = x + 1;
                }
            }

            List<Integer> l = new ArrayList<>(divisors);
            JOptionPane.showMessageDialog(null, "The divisors of " + i + " are:\n" + l);
        }
    }

【讨论】:

  • 看来 OP 不想要 List 或 ArrayList 解决方案,因为这超出了 OP 类的范围。此外,这个答案没有对代码进行任何解释。
  • OP 不想要的是他的问题,而不是他可以在不进入 List 或 ArrayList 或 Set 或任何集合类的情况下取得进步。解释,是的,我会提供。
  • @deepakmarathe 并不是我不想要它,我的导师检查了我们的代码,他已经抨击了一些人使用他没有提到的工具。我知道这很糟糕,但总比没有好......
【解决方案4】:

使用 ArrayList 创建动态数组。
下面的代码将为您提供帮助。
程序中需要改变的地方。

  1. 导入 java.util.*;
  2. 获取一个 ArrayList 变量
  3. 在 Arraylist 对象上调用 toString 方法
import java.util.*;
import javax.swing.JOptionPane;

public class NewClass3 {
    public static void main(String[] args) {
        String n = JOptionPane.showInputDialog(null, "Enter a natural number");
        Integer i = Integer.parseInt(n);

        int d = i - 1;
        int x = 2;
        List<Integer> dvr = new ArrayList<>();
        while (x >= 2 && x <= d) {
            double y = i % x;

            if (y == 0) {
                dvr.add(x);
                x=x+1;
            } else {
                x = x + 1;
            }
        }

        JOptionPane.showMessageDialog(null, "The divisors of " + i + " are:\n" + dvr.toString());
    }
}

【讨论】:

    最近更新 更多