java布尔方法

Ahm*_*wod -2 java methods boolean

我的教科书中有一个问题:

编写一个方法倍数,它将两个整数作为参数,如果第一个整数可以被第二个整数均分,则返回true(即,除法后没有余数); 否则,该方法应返回false.将此方法合并到一个应用程序中,使用户可以输入值来测试方法.

我写了这段代码,但它不起作用:

public class project1 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int a, b;
        System.out.println("enter the first number");
        a = input.nextInt();

        System.out.println("enter the first number");
        b = input.nextInt();
    }

    public static boolean multiple(int g, int c) {

        int g, c;

        if (g % c = 0) {
            return true;
        } else {
            return false;

        };
    }
}
Run Code Online (Sandbox Code Playgroud)

Kar*_*k T 5

//int g, c;
^^
Run Code Online (Sandbox Code Playgroud)

删除此行..


if (g%c==0){
        ^
Run Code Online (Sandbox Code Playgroud)

您需要==用于检查相等性.


您实际上可以执行以下操作以减少几行.

public static boolean multiple(int g, int c) {
    return g%c == 0;
}
Run Code Online (Sandbox Code Playgroud)