小编Sha*_*nix的帖子

一个数的除数列表

这是我的第一篇文章因此不要踩我,如果我写了一些愚蠢的事.

我刚刚开始上课,今天在"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 …
Run Code Online (Sandbox Code Playgroud)

java arrays while-loop joptionpane

5
推荐指数
1
解决办法
1478
查看次数

标签 统计

arrays ×1

java ×1

joptionpane ×1

while-loop ×1