※J.Y Chen 的個人部落格 ※

Just Follow Our Heart And We will shine!

58 瀏覽人次

【程式語言】JAVA的第五堂課 |算出二維陣列中 奇數 跟 偶數分別有幾個 (odd/even)

Published: in JAVA by .

public class inclass
{

  public static void main(String[] arg)
  {
    
    int[][] num = {{3,4,10,2,18},{3,1,6,7,9},{1,4,10,11,5}};
	int odd = 0 , even = 0;
	for (int i = 0 ; i<3 ; i++) {
		for (int j = 0 ; j<5 ; j++) {
		if(num[i][j] % 2 == 1) {
			odd += 1 ;
			}
		else {
			even +=1;
			}
		}
	}
	System.out.println("odd=" + odd +",even=" + even);
  }
}

result:

©2019 - 2024 Henry Chen