这是我到目前为止所拥有的。如果我在第一个数字中输入4,在第二个数字中输入6,则表示4是6的倍数,这是不正确的。我不确定这是怎么回事。

    System.out.print("\n Name =");
    String userName = console.readLine();

    System.out.print("\n First Number =");
    int firstNumber = console.readInt();

    System.out.print("\n Second Number =");
    int secondNumber = console.readInt();

    if (firstNumber / secondNumber == 0 || secondNumber / firstNumber == 0) {
        System.out.print("\n" + userName + "," + firstNumber + " is a multiple of " + secondNumber);
    }
    else {
        System.out.print("\n" + userName + "," + firstNumber + " is not a multiple of " + secondNumber);
    }

最佳答案

检查一个数字是否是另一个数字的倍数的最简单方法是使用模(除法后的余数)运算。因此,代码可能如下所示:

if(firstNumber % secondNumber == 0) {
    // I'm a multiple!
}


请记住要检查secondNumber是否不为零,否则将被除以零!

10-07 22:34