Java: “error: cannot find symbol”

前端 未结 3 884
借酒劲吻你
借酒劲吻你 2021-01-18 19:56

(Rookie mistake, I\'m sure.)

I\'m a first year computer science student, and attempting to write a program for an assignment, with the code;

import          


        
3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-18 20:29

    The n variable was declared in the main method and thus is visible only in the main method, nowhere else, and certainly not inside of the calcNumFactors method. To solve this, give your calcNumFactors method an int parameter which would allow calling methods to pass an int, such as n into the method.

    public static void calcNumFactors(int number) {
       // work with number in here
    }
    

    and call it like so:

    int n = keyboard.nextInt(); 
    calcNumFactors(n);
    

提交回复
热议问题