java - 范围内整数的倍数

标签 java

我正在做一个学校项目,但我有点被阻止了。 我期待构建一个 JavaScript,它要求用户输入 1 到 20 之间的数字,然后查找并列出该数字在 0 到 100 范围内的所有倍数。

目前的情况如下:

public static void main(String[] args) {
    Scanner lector = new Scanner(System.in);
    System.out.println("*** Program start ***\n");
    System.out.println("Insert number [integer, between 1 and 20]: ");
    boolean okay = false;

    while (!okay) {
        int n1 = lector.nextInt();
        lector.nextLine();
        if (n1<1 || n1>20) {
            System.out.print("Invalid number!\nplease try again [between 1 and 20]: ");
        } else {
            okay = true;
            System.out.println("Number accepted!");
        }

    int i = 0;
    while (i <= 100) {
       System.out.println(i);

       if ((n1%100) == 0) {
           System.out.println(n1);
       }
       i = i + 1;
    }
    System.out.println("\n*** End ***");
    }
    }
}

我的数学显然很差,因为我无法让公式发挥作用。

提前谢谢您!

最佳答案

public static void main(String[] args) {
    Scanner lector = new Scanner(System.in);
    System.out.println("*** Program start ***\n");
    System.out.println("Insert number [integer, between 1 and 20]: ");
    boolean okay = false;

    while (!okay) {
        int n1 = lector.nextInt();
        lector.nextLine();
        if (n1<1 || n1>20) {
            System.out.print("Invalid number!\nplease try again [between 1 and 20]: ");
        } else {
            okay = true;
            System.out.println("Number accepted!");
        }

    int i = 0;
    while (i <= 100) {
       System.out.println(i);

       if ((n1%i) == 0) {
           System.out.println(i);
       }
       i = i + 1;
    }
    System.out.println("\n*** End ***");
    }
    }
}

关于java - 范围内整数的倍数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36307786/

相关文章:

java - 数据库对象POJO中的文件(Springboot)

java - 突出显示 SWT 树节点中的特定字符串

java - 在单独线程中定义的 Paint 函数不绘图(Java)

java - 在 Android 中使用 Socket.io 进行聊天应用

java - ClassLoader 可以用任何东西替换数组

java - 如何修复某些手机中每秒发送数据的位置?

java - 如何在android中的按钮文本上添加阴影?

java - 安卓工作室 : "Gradle sync failed: Could not run JVM from the selected JDK."

java - 为什么 Jar 的依赖项没有在 list 中列出

java - 数据库查询中断