物件導向程式設計

 

 

      2.9 餘數運算子

 

 


授課教師:陳慶瀚

WWW : http://www.miat.ee.isu.edu.tw/java

E-mail : pierre@isu.edu.tw   

 


Java has one important arithmetical operator you may not be familiar with, %, also known as the modulus or remainder operator. The % operator returns the remainder of two numbers. For instance 10 % 3 is 1 because 10 divided by 3 leaves a remainder of 1. You can use % just as you might use any other more common operator like + or -.

 

class Remainder {

  public static void main (String args[]) {

    int i = 10;
    int j = 3;
    int k;

    System.out.println("i is " + i);
    System.out.println("j is " + j);
  
    k = i%j;
    System.out.println("i%j is " + k);
  }

} 
Here's the output:

 

C:> javac Remainder.java
C:> java Remainder
i is 10
j is 3
i%j is 1
C:>

 


課堂練習:

設計一個程式,讓使用者輸入一個整數值,輸出該整數除7的餘數

 


 

物件導向程式設計

義守大學電機系 陳慶瀚

2001.10.02