ืืื ืืืืงืช ืืกืคืจืื ืฉืืืื ื-Java?
ืืืืงื ืื'ืืืื ืืชืจืืฉืช ืืืจื ืืื ืืื ืืืืงื ืจืืืื ืืืชืืืืงื ืื ืืืืื ืืืืืชืืื. ืขื ืืืช, ืื ืคืฉืื ืืฉืืื ืืช ืืฉืืจ. ืืืืืื, ืื ืืชื ืืืืง 9 ื-2, ืืื ื ืืื 4 ืืืฉืืจ ืืื 1. ืืืืื ืืืืืชืืื, ืืชืฉืืื ืืื 4.5 ืื 4ยฝ. ืื ืชืืฆืข ืืช ืืืชื ืืืฉืื ืขื int ื-Java, ืืชืฉืืื ืฉืื ืชืืื 4. ืื ืื ืืขืืื ืืืกืคืจ ืืฉืื ืืงืจืื ืืืืชืจ (ืืื ~4.5 = 5) 1 ืฉืื ืฉืืจืืช ื ืืจืงืช.
ืืืืื 1 [ืืฉืืจ ืืื 0]
ืืืืงืช ืืืกืคืจืื ืืฉืืืื ื-Java ืคืืขืืช ืืฆืืจื ืืืฉืืืช ืขืืืจ ืื ืืืงืจืื ืืื ืืืืืง ืืืืง ืืืืืืื ืืช ืืืืืืื ื (ืืกืคืจ ืฉืื ืืืงื x ืฉืื). ืืชืฉืืื ืืื ืืกืคืจ ืฉืื ืืกืื ืื ืชืื ืื ืฉื ืืกืคืจ ืฉืื ืืืื ืืืืืืง ืืืชื ืืื ืืฆืคื. ืืืื ืฉืืื ืืืืื ื ืชืื ืื. ืืืืืื, ืขืืื ืืงืืข ืืื.public class IntegerDivision {
public static void main(String[] args) {
int dividend = 100;
int divisor = 5;
int quotient = dividend / divisor;
//Dividend completely divides the divisor
System.out.println(dividend + " / " + divisor + " = " + quotient);
dividend = 143;
divisor = 11;
quotient = dividend / divisor;
//Dividend completely divides the divisor
System.out.println(dividend + " / " + divisor + " = " + quotient);
}
}
ืชึฐืคืึผืงึธื
100 / 5 = 20 143 / 11 = 13
ืืืืื 2 [ืืฉืืจ ืืื ื 0]
ืขืืืจ ืื ืืงืจื ืืืืืงื ืฉืืื ืืืชืจื ืืื ื 0, ืืชืืฆืื ืืกืืคืืช ืชืืงืฆืฅ ืืืกืคืจ ืืฉืื ืื ืืืง ืืืืื ืืืืชืจ (9/2 = 4). ืื ืืืฆื ืืืืืื ืืงืจืืื. ืขืฉืืืื ืืืืืช ืืงืจืื ืฉืืื ืืชื ืฆืจืื ืืช ืืื ื ืืคืืขื ืืขืฉืจืื ืืช. ืืืงืจื ืื, ืืชื ืืืื ืืืฉืชืืฉ ืืกืื ืื ืชืื ืื ืฆืฃ ืื ืืคืื. ืขื ืืืช, ืื ืืจืฆืื ื ืืขืื ืืช ืืื ื ื-int ืืงืจืื ืืืืชืจ, ืชืืื ืืขืฉืืช ืืช ืืคืขืืืืช ืืืืืช.public class IntegerDivision {
public static void main(String[] args) {
int dividend = 9;
int divisor = 2;
int quotient = dividend / divisor;
// Case I - Dividend does not divide the divisor completely
// The quotient is chopped / truncated
System.out.print("Integer division \t\t" );
System.out.println(dividend + " / " + divisor + " = " + quotient);
// Case II - Mathematical or real life division
// Use float or double data type to get the actual quotient
double actualQuotient = (double)dividend / divisor;
System.out.print("Mathematics division \t\t" );
System.out.println((double)dividend + " / " + divisor + " = " + actualQuotient);
// Case III - Integer Division with rounding off
// the quotient to the closest integer
long roundedQuotient = Math.round((double)dividend / divisor);
System.out.print("Round off int division \t\t" );
System.out.println((double)dividend + " / " + divisor + " = " + roundedQuotient);
}
}
ืชึฐืคืึผืงึธื
ืืืืงื ืฉืืืื 9 / 2 = 4 ืืชืืืืงื ืืืืงื 9.0 / 2 = 4.5 ืกืืืื ืืื ื ืืืืงื 9.0 / 2 = 5
ืึถืกืึผึตืจ
ืืงืจื I ืืืงืจื II ืืืื ืื ืืืืืื. ืขืืืจ ืืงืจื III, ืืชื ืืืื ืืคืจืง ืืืชื ืืฉืืืื ืืืืื.-
ืจืืฉืืช, ืขืืื ืืืืืจ ืืช ืืืืืืื ื ืืืคืื.
-
ืืฆืข ืืช ืืืืืงื ืืจืืืื ืฉื Java int.
-
ืขืืื ืืช ืืื ื ืืืืฆืขืืช ืฉืืืช Math.round() .
-
ืืฉืชืืฉ ืืกืื ื ืชืื ืื ืืจืื ืืื ืืืืกื ืืช ืืื ื ืืืขืืืืช.
-
ืื ื ืื! ืืฉ ืื ืืช ืืคืื ืืจืฆืื ืฉืื ืืชืืจ ืืื ื.
GO TO FULL VERSION