Java Long.remainderUnsigned()
方法
原文:https://www.studytonight.com/java-wrapper-class/java-long-remainderunsigned-method
Java remainderUnsigned()
方法属于Long
类。此方法用于返回第一个参数除以第二个参数得到的余数(无符号)。结果,即余数总是作为无符号值。
语法:
public static long remainderUnsigned(long dividend, long divisor)
参数:
传递的参数将是long dividend
将被划分和 long divisor
将进行划分过程.
返回:
当第一个参数(即被除数)除以第二个参数(即除数)时,返回无符号余数。
例 1:
这里,除法过程用正值和负值进行,得到的余数是无符号值。
import java.lang.Long;
public class StudyTonight {
public static void main(String[] args) {
long a = 100L;
long b = 5L;
long c = -3L;
System.out.println("Remainder of\t" + a + "/" + b + "\t is \t" + Long.remainderUnsigned(a, b));
System.out.println("Remainder of\t" + a + "/" + c + "\t is \t" + Long.remainderUnsigned(a, c));
}
}
100/5 的余数为 0 100/-3 的余数为 100
例 2:
这里有一个用户定义的例子,任何使用这段代码的人都可以输入自己选择的值,并获得等效的输出。
import java.util.Scanner;
public class StudyTonight {
public static void main(String[] args) {
try {
System.out.print("Enter the Dividend and Divisor ");
Scanner sc = new Scanner(System. in );
long Dividend = sc.nextLong();
long Divisor = sc.nextLong();
long result = Long.remainderUnsigned(Dividend, Divisor); //return the unsigned remainder
System.out.println("Remainder is: " + result);
}
catch(Exception e) {
System.out.println("Invalid Input!!");
}
}
}
输入被除数和除数 87 9 余数为:6 *输入被除数和除数 333 -11 余数为:333 *输入无效!!
实时示例:
在这里,您可以测试实时代码示例。您可以为不同的值执行示例,甚至可以编辑和编写您的示例来测试 Java 代码。