【发布时间】:2019-10-10 01:16:04
【问题描述】:
我的作业是要求我使用定义的范围(通过输入范围中的最高数字)和用户输入的除数来查找范围内可被除数整除的所有数字,而不使用数学或赋值运算符++、- 或 = 除外(不允许使用 +、-、/、*、%、+=、%= 等)。
使用数学运算符编写该程序非常容易,但是当我尝试仅使用增量运算符编写它时,我每次都迷失了方向。我对编程非常陌生。
下面是我到目前为止的代码,但是它在除数之后打印范围(1到最大数字)中的每个数字(所以如果用户输入的除数是5并且最大数字是20,它将打印每个数字 5-20),而不仅仅是能被除数整除的数字 (5, 10, 15, 20)。
input = new Scanner(System.in);
// input the ending number
System.out.println("Enter the ending number: ");
n = input.nextInt();
System.out.println("Enter the divisor: ");
count = input.nextInt();
variable = 0;
System.out.println("Below are all the numbers that are evenly divisible by " + count + " from 1 up to " + n);
while (count <= n){
variable++;
if (variable == count){
System.out.print(count + " ");
count++;}
【问题讨论】:
标签: java loops if-statement increment