Java Integer.remainderUnsigned()
方法
原文:https://www.studytonight.com/java-wrapper-class/java-integer-remainderunsigned-method
Java remainderUnsigned()
方法属于Integer
类。此方法用于返回第一个参数除以第二个参数得到的余数(无符号)。结果,即余数总是作为无符号值。
语法:
public static int remainderUnsigned(int dividend, int divisor)
参数:
传递的参数将是int dividend
将被划分和 int divisor
将进行划分过程.
返回:
第一个参数(即被除数)除以第二个参数(即除数)时的无符号余数。
例 1:
这里,正常的除法过程发生在所有整数都有符号的时候。
import java.lang.Integer;
public class StudyTonight
{
public static void main(String[] args)
{
int a = 100;
int b = 5;
int c = 3;
System.out.println("Remainder of\t" + a+ "/" +b+ "\t is \t" +Integer.remainderUnsigned(a, b));
System.out.println("Remainder of\t" + a+ "/" +c+ "\t is \t" +Integer.remainderUnsigned(a, c));
}
}
100/5 的余数为 0 100/3 的余数为 1
例 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);
int Dividend = sc.nextInt();
int Divisor = sc.nextInt();
int result = Integer.remainderUnsigned(Dividend, Divisor); //return the unsigned remainder
System.out.println("Remainder is: "+result);
}
catch(Exception e)
{
System.out.println("Invalid Input!!");
}
}
}
输入被除数和除数 78 9 余数为:6 *输入被除数和除数-56 9 余数为:2 *输入被除数和除数 100 0 无效输入!!
实时示例:
在这里,您可以测试实时代码示例。您可以为不同的值执行示例,甚至可以编辑和编写您的示例来测试 Java 代码。