一个数的除数列表

List of divisors of a number

这是我的第一个 post 所以 DO 如果我写了一些愚蠢的东西,请踩我。

我刚刚开始 IT classes,今天 "while" 循环 class 我的导师给了我们以下作业:

Write a program which reads a natural number n and displays in one graphical box all its divisors from the interval [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));
    }
}

问题是循环用很多零填充数组,导师结果的屏幕截图显示 window 仅列出除数。

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

非常感谢任何帮助。

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

由于您将数组的大小定义为 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));
}
}

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

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

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

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

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

然后当您的主循环完成后,您可以创建一个新的 "display array",它只包含除数,而不包含零。此时,index 告诉你到底需要多大:

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

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);
        }
    }

使用 ArrayList 创建动态数组。
下面的代码将帮助你。
在您的计划中需要更改的内容。

  1. import java.util.*;
  2. take an ArrayList varible
  3. call toString method on Arraylist Object
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());
    }
}