有 Java 编程相关的问题?

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

java给定两个数字,如果其中任何一个除以另一个,则返回true,否则返回false

给定两个数字,如果其中任何一个除以另一个,则返回true,否则返回false

public class DividesAB {

static int testcase11 = 208;
static int testcase12 = 7;
boolean aDivisblebyb, bDivisblebya, answer;

    public static void main(String args[]){
        DividesAB testInstance = new DividesAB();
        boolean result = testInstance.divides(testcase11,testcase12);
        System.out.println(result);
    }

    //write your code here
    public boolean divides(int a, int b){
        boolean aDivisiblebyb = a%b == 0;
        boolean bdivisiblebya = b%a == 0;
        boolean answer = aDivisiblebyb||bDivisiblebya;
        return answer;
    }
} 

我一直收到类似cannot find symbol的错误


共 (2) 个答案

  1. # 1 楼答案

    你有一堆乱七八糟的代码,其中一半是不需要的。要查找符号是否未定义,请查看IDE中它抱怨的代码行,并查看该变量不在范围内的原因

    如果你只写你需要的代码,那么犯错误的机会就少了,而且更容易看出错误在哪里

    我就是这样写的

    public class DividesAB {
        public static void main(String[] args) {
            int a = 208, b = 7;
            System.out.printf("a: %,d divides b: %,d is %s%n", divides(a, b));
        }
    
        //write your code here
        public static boolean divides(int a, int b){
            return a % b == 0 || b % a == 0;
        }
    } 
    
  2. # 2 楼答案

    变量名不正确

    aDivisblebyb and you are using aDivisiblebyb

    因此,更改变量名应该会起作用