java中%(模数)和/(除数)的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41552786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:28:59  来源:igfitidea点击:

difference between % (modulus) and / (divided) in java?

javamath

提问by sreeku24

Yesterday i had face an interview, in that interview they asked me a one question like tell me the difference between % and / ?
I know the result what they gave, but i cont able to explain it theoretically Will you please give me any body how can i explain it theoretically, i also done a program on this

昨天我接受了面试,在那次面试中他们问了我一个问题,比如告诉我 % 和 / 之间的区别?
我知道他们给出的结果,但我无法从理论上解释它你能不能给我任何身体我如何从理论上解释它,我也做了一个关于这个的程序

class FindDifference 
{
    public static void main(String[] args) 
    {
        int num1=12;
        int num2=3;
        int modulus=num1%num2;
        int divide=num1/num2;
        System.out.println("modulus :"+modulus);
        System.out.println("division :"+divide);
    }
}

and my result is:

我的结果是:

modulus :0    
division :4    

But how can i Give the theoretically explanation between them.Will you please help me

但是我怎么能给出他们之间的理论上的解释。请你帮帮我

采纳答案by sabbahillel

The percent symbol is the modulus operator. I would say that they wanted you to recognize the difference that the division operator on integer inputs gives an integer result. The modulus operator gives the remainder in the sense that the original value is recovered from the integer arithmatic as

百分比符号是模运算符。我会说他们想让你认识到整数输入上的除法运算符给出整数结果的区别。在从整数算术中恢复原始值的意义上,模运算符给出余数为

(num1/num2)*num2 + num1%num2 == num1

(num1/num2)*num2 + num1%num2 == num1

This is the theoretical explanation.

这是理论上的解释。

An example (using python for ease of typing) gives

一个例子(使用 python 以便于打字)给出

>>> -21 %4
3
>>> -21 /4
-6
>>> -6*4 +3
-21

Your Java program above would give you. (I do not have a Java compiler on this machine).

你上面的Java程序会给你。(我在这台机器上没有 Java 编译器)。

class FindDifference 
{
    public static void main(String[] args) 
    {
        int num1 = 10;
        int num2 = 3;
        int percentage = num1%num2;
        int divide = num1/num2;
        int resume = divide*num2 + percentage
        System.out.println("Percent :"+percentage);
        System.out.println("division :"+divide);
        System.out.println("resume :"+resume);
    }
}