package chap1;

public class t1225 {
 public static void main(String args[]) throws Exception {

  
  int i=0,j=0;  //i重複宣告15次  j重複宣告3次
  for (int k = 0; k < 3; k++) {
   System.out.println("k= " + k);
  }
  for (j = 0; j < 5; j++) {  //外部loop  內外迴圈宣告變數(i.j)必須不同
   System.out.print("j= " + j+"\t");
   for ( i = 0; i < 5; i++) {  //內部loop
           // 5*5=25次
    System.out.print(i+"\t");
   }
   System.out.println();
  }  
 }
}

---

package chap1;

public class t1225 {
 public static void main(String args[]) throws Exception {

  int data[][]=new int[10][10];
  
  int i=0,j=0;  //i重複宣告15次  j重複宣告3次
  
  for (j = 0; j < 5; j++) {  //外部loop  內外迴圈宣告變數(i.j)必須不同
   System.out.print("j= " + j+"\t");
   for ( i = 0; i < 5; i++) {  //內部loop
           // 5*5=25次
    System.out.print(i+"\t");
    data[0][i]=0*i;
    System.out.println("data[0]["+i+"]="+data[0][i]);
   }
   System.out.println();
  }  
 }
}

 ---

package chap1;

public class t1225 {
 public static void main(String args[]) throws Exception {

  int data[][]=new int[10][10];
  int d1[]={10,11,12};
  int d2[][]={{1,2,3},{4,5,6},{7,8,9}};
  int i=0;  //i重複宣告20次  j重複宣告4次
  
  for (int j = 0; j < 4; j++) {  //外部loop  內外迴圈宣告變數(i.j)必須不同

   for ( i = 0; i < 5; i++) {  //內部loop
           // 5*5=25次
    data[j][i]=j*i;
    System.out.print(data[j][i]+"\t");
    
   }
   System.out.println();
  }  
 }
}

 

---

 

package chap1;

public class t1225 {
 public static void main(String args[]) throws Exception {

  int d1[]={10,11,12};
  int d2[][]={{1,2,3},{4,5,6},{7,8,9}};
  int i=0,num = 0;         //i重複宣告20次  j重複宣告4次
  
  for ( int j = 0; j < d2.length; j++) { //外部loop  內外迴圈宣告變數(i.j)必須不同

  for ( i = 0; i < d2[j].length; i++) {  //內部loop
           
  for( i = 0;i<3;i++){
   num=num+d2[j][i]*d1[i];
   }
   System.out.println("num="+num);
  }  
 }}}

 

 -----------練習題

 

package chap1;

public class t1225 {
 public static void main(String args[]) throws Exception {

  int d1[][]={{10,13},{11,14},{12,15}};
  int d2[][]={{1,2,3},{4,5,6},{7,8,9}};
  int i=0,j=0,k=0,num[][]= new int [3][2];      //i重複宣告20次  j重複宣告4次
  
  for( k = 0;k<2;k++){   //同樣的得執行兩次
   
  for (j = 0; j < d2.length; j++) { //外部loop  內外迴圈宣告變數(i.j)必須不同

  for ( i = 0; i < d2[j].length; i++) {  //內部loop
   
   num[j][k]=num[j][k]+d2[j][i]*d1[i][k];
   }
   System.out.println("num["+j+"]["+k+"]="+num[j][k]);
  }  
 }}}

 

 

 

 

arrow
arrow
    全站熱搜

    玥君 發表在 痞客邦 留言(0) 人氣()