这是我的第一篇文章因此不要踩我,如果我写了一些愚蠢的事.
我刚刚开始上课,今天在"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)