我不明白如何使用模数

I don't understand how to use Modulus

我是编码的新手,我在让它工作时遇到了很多麻烦:

声明 2 个整型变量。提示用户输入两个数字。 使用 Scanner 对象将值存储到变量中。 如果第二个数是第一个数的倍数,则显示“是”的倍数。否则显示"不是"的倍数

我有下面这段代码然后我迷路了:

int number1, number2;
System.out.println("Enter a number:");
number1 = keyboard.nextInt();
System.out.println("Enter a Number:");
number2 = keyboard.nextInt();

模数是2个数相除的余数。如果number2是number1的倍数,相除余数为0,否则不是。

package test;

import java.util.Scanner;

public class MyTest {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);

        int number1, number2;
        System.out.println("Enter a number:");
        number1 = keyboard.nextInt();
        System.out.println("Enter a Number:");
        number2 = keyboard.nextInt();

        int mod = number2 % number1;

        if (mod == 0) {
            System.out.println(number2 + " is a multiple of " + number1);
        } else { 
            System.out.println(number2 + " is not a multiple of " + number1);
        }
    }

}

模数只是给你两个数相除的余数。例如:4/3 以类似的编程方式将余数给出为 1,您可以尝试 4%3,这两个将给出相同的响应。在下面添加了 python 代码片段:

a = int(input("Enter first no."))
b = int(input("Enter second no."))
print("Modulus operation a%b gives : ", (a % b))
print("Is ", a, " multiple of ", b)
print((a%b) == 0)

如果 number1 是 number2 的倍数,则除以 number2 的余数为 0, 例如,如果 4 是 2 的倍数而不是 4%2==0,则为真, 这里 % 是模运算符 returns 两个数相除的余数 :::

在你的例子中,如果 number1 是 number2 的倍数,则 Number1%number2==0(必须);

你的程序应该是这样的:

If(number1%number2==0)
{
System.out.println("number1 is a multiple of number 2");
 }
Else
 System.out.println("not a multiple");

i am lost

假设你参加了一场吃巧克力的比赛,规则是:

同时吃三个巧克力

如果给你:

1 chocolate,  you will not eat, and 1 chocolate  will be left
2 chocolates, you will not eat, and 2 chocolates will be left
3 chocolates, you eat once,     and 0 chocolates will be left
4 chocolates, you eat once,     and 1 chocolate  will be left
5 chocolates, you eat once,     and 2 chocolates will be left
6 chocolates, you eat twice,    and 0 chocolates will be left
7 chocolates, you eat twice,    and 1 chocolate  will be left
...
100 chocolates, you eat 33 times, 1 chocolate will be left
...
1502 chocolates, you eat 500 times, 2 chocolates will be left

每次剩下的巧克力数叫做模数是数学,计算机中强大的运算

如您所知,任何时候都只会剩下不到 3 块巧克力(012)。

因此,x modulus y0y-1 的范围内总是小于 y

在编程中,modulus运算符是%

所以,如果我们使用 modulus 运算符 % 来写出你吃巧克力的方式,它看起来像:

1 % 3  =  1
2 % 3  =  2
3 % 3  =  0
4 % 3  =  1
5 % 3  =  2
6 % 3  =  0
7 % 3  =  1
...
100 % 3  =  1
...
1502 % 3  =  2

现在告诉我,你迷路了吗?