먼저 두 개의 숫자를 설정합니다.
int one = 250; int two = 200;
이제 그 숫자를 다음 함수에 전달하십시오.
public int RemainderFunc(int val1, int val2) {
if (val2 == 0)
throw new Exception("Second number cannot be zero! Cannot divide by zero!");
if (val1 < val2)
throw new Exception("Number cannot be less than the divisor!");
else
return (val1 % val2);
} 위에서 우리는 두 가지 조건을 확인했습니다.
- 두 번째 숫자가 0이면 예외가 발생합니다.
- 첫 번째 숫자가 두 번째 숫자보다 작으면 예외가 발생합니다.
두 숫자의 나머지를 반환하려면 다음은 전체 코드입니다.
예
using System;
namespace Program {
class Demo {
public int RemainderFunc(int val1, int val2) {
if (val2 == 0)
throw new Exception("Second number cannot be zero! Cannot divide by zero!");
if (val1 < val2)
throw new Exception("Number cannot be less than the divisor!");
else
return (val1 % val2);
}
static void Main(string[] args) {
int one = 250;
int two = 200;
int remainder;
Console.WriteLine("Number One: "+one);
Console.WriteLine("Number Two: "+two);
Demo d = new Demo();
remainder = d.RemainderFunc(one, two);
Console.WriteLine("Remainder: {0}", remainder );
Console.ReadLine();
}
}
} 출력
Number One: 250 Number Two: 200 Remainder: 50