gpt4 book ai didi

java - 范围内整数的倍数

转载 作者:行者123 更新时间:2023-12-01 10:08:21 25 4
gpt4 key购买 nike

我正在做一个学校项目,但我有点被阻止了。我期待构建一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com