有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java理解除数程序中的选择和循环

我的家庭作业要求我使用定义的范围(通过输入范围中的最高数字)和用户输入的除数来查找该范围内可被除数整除的所有数字,而不使用数学运算符或赋值运算符,除了++,-,或=(+,-,/,*,%,+=,%=,%=,等等是不允许的)

这个程序非常容易用数学运算符编写,但当我尝试只用增量运算符编写时,每次都会迷路。我对编程非常陌生

下面是我到目前为止的代码,但它会打印除数后(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++;}

共 (1) 个答案

  1. # 1 楼答案

    反复数数, 现在,对于奇数和偶数的最大值,下面应该可以使用了

     int temp = count;
        while(count<n)
        {
    
            for(int i=0; i<temp; i++)
            {
                 variable++;
    
            }
            if(variable>n) break;
            else {
            count = variable;
            System.out.println(count);
            }
    
        }