《軟體工程》第2次作業(1、個人專案實踐)

GEM茶度餘生發表於2015-09-21

1. 設計一個類(不妨叫做A ),實現對一維陣列的指定範圍內的元素相加運算。

 1 import java.util.Scanner;     //匯入scanner包,用scanner類從鍵盤輸入資料
 2   
 3 public class yiwei {
 4     public static void main(String[]args)
 5     {
 6           int[] aa={9680,-3756,-5569,3402,4704,-6267,-2393,3191,-965,-5042,-6951,-6184,-8752,525,2144,6195,541,-3336,6904,-3130,-3261,-4770,-34,-1991,292,3703,4590,7406,2272,5190,-5336,-1521,4381,-1579,4700,269,-1059,-2249,-166,-2553,-1098,-6839,-4172,303,7838,256,-931,-5243,-51,267,-3432,2351,58,49,5963,253,3616,-7918,-4486,1256,-3305,-5528,-5891,3779,-4004,950,-8741,1864,-1158,871,-2899,5178,3183,-784,-7603,-984,-2407,-3411,-1467,4295,1271,-778,-2478,-2442,4041,-1031,1999,973,-3647,3312,-4773,2333,-719,-4644,1092,-4944,4525,3831,4107,-1816};
 7           int sum=0;
 8           Scanner a = new Scanner(System.in);            
 9           System.out.println("從第幾個開始(<=100):");
10           int begin = a.nextInt() ;
11           System.out.println("到第幾個結束(<=100):");
12           int over =a.nextInt();
13           System.out.println(""+begin+"個元素到第"+over+"個元素的和為:");
14           
15           for(int i=begin-1;i<=over-1;i++)//一定要減1,因為第一個是aa[0]
16               sum+=aa[i];
17               System.out.println(aa[begin-1]+"+...+"+aa[over-1]+"="+sum);
18               
19     }
20 }

注:此時按照表格內行數從176~275 行

執行結果:

驗證:

2. 設計一個類(不妨叫做B ),實現對二維陣列的指定範圍內的元素相加運算。

 1 import java.util.Scanner;
 2   public class erwei {
 3   public static void main(String[]args){
 4   int[][]b={
 5             {9680,-3756,-5569,3402,4704,-6267,-2393,3191,-965,-5042,-6951,-6184,-8752,525,2144,6195,541,-3336,6904,-3130,-3261,-4770,-34,-1991,292,3703,4590,7406,2272,5190,-5336,-1521,4381,-1579,4700,269,-1059,-2249,-166,-2553,-1098,-6839,-4172,303,7838,256,-931,-5243,-51,267,-3432,2351,58,49,5963,253,3616,-7918,-4486,1256,-3305,-5528,-5891,3779,-4004,950,-8741,1864,-1158,871,-2899,5178,3183,-784,-7603,-984,-2407,-3411,-1467,4295,1271,-778,-2478,-2442,4041,-1031,1999,973,-3647,3312,-4773,2333,-719,-4644,1092,-4944,4525,3831,4107,-1816},
 6             {9680,-3756,-5569,3402,4704,-6267,-2393,3191,-965,-5042,-6951,-6184,-8752,525,2144,6195,541,-3336,6904,-3130,-3261,-4770,-34,-1991,292,3703,4590,7406,2272,5190,-5336,-1521,4381,-1579,4700,269,-1059,-2249,-166,-2553,-1098,-6839,-4172,303,7838,256,-931,-5243,-51,267,-3432,2351,58,49,5963,253,3616,-7918,-4486,1256,-3305,-5528,-5891,3779,-4004,950,-8741,1864,-1158,871,-2899,5178,3183,-784,-7603,-984,-2407,-3411,-1467,4295,1271,-778,-2478,-2442,4041,-1031,1999,973,-3647,3312,-4773,2333,-719,-4644,1092,-4944,4525,3831,4107,-1816}
 7            };
 8   int sum=0;
 9   Scanner bb = new Scanner(System.in);
10   System.out.println("從第幾行開始:");//<=2
11   int beginI = bb.nextInt(); 
12  System.out.println("到第幾行結束:");//<=2
13  int overI =bb.nextInt();
14  System.out.println("從第幾列開始:");//<=100
15  int beginJ = bb.nextInt(); 
16  System.out.println("到第幾列結束:");//<=100
17  int overJ =bb.nextInt();
18  System.out.println(""+beginI+"行第"+beginJ+"列到第"+overI+"行第"+overJ+"列的和為:");
19  
20  for(int i=beginI-1;i<=overI-1;i++)//不減1的話2會溢位
21  {
22      for(int j=beginJ-1;j<=overJ-1;j++)//不減1的話100會溢位
23      {
24      sum+=b[i][j];
25      }
26  }
27  System.out.println(b[beginI-1][beginJ-1]+"+...+"+b[overI-1][overJ-1]+"="+sum);  
28  }
29  }

注:此時為了節約時間是把第一列的資料用了兩遍

執行結果:

3. 設計一個類(不妨叫做C ),實現對三維陣列的指定範圍內的元素相加運算。

 1 import java.util.Scanner;
 2   
 3   public class sanwei {
 4  
 5      
 6      public static void main(String[]args)
 7      {
 8          int sum=0;
 9           int[][][] cc = new int[][][]
10              {
11                  {{9680,-3756,-5569,3402,4704,-6267,-2393,3191,-965,-5042,-6951,-6184,-8752,525,2144,6195,541,-3336,6904,-3130,-3261,-4770,-34,-1991,292,3703,4590,7406,2272,5190,-5336,-1521,4381,-1579,4700,269,-1059,-2249,-166,-2553,-1098,-6839,-4172,303,7838,256,-931,-5243,-51,267,-3432,2351,58,49,5963,253,3616,-7918,-4486,1256,-3305,-5528,-5891,3779,-4004,950,-8741,1864,-1158,871,-2899,5178,3183,-784,-7603,-984,-2407,-3411,-1467,4295,1271,-778,-2478,-2442,4041,-1031,1999,973,-3647,3312,-4773,2333,-719,-4644,1092,-4944,4525,3831,4107,-1816},{9680,-3756,-5569,3402,4704,-6267,-2393,3191,-965,-5042,-6951,-6184,-8752,525,2144,6195,541,-3336,6904,-3130,-3261,-4770,-34,-1991,292,3703,4590,7406,2272,5190,-5336,-1521,4381,-1579,4700,269,-1059,-2249,-166,-2553,-1098,-6839,-4172,303,7838,256,-931,-5243,-51,267,-3432,2351,58,49,5963,253,3616,-7918,-4486,1256,-3305,-5528,-5891,3779,-4004,950,-8741,1864,-1158,871,-2899,5178,3183,-784,-7603,-984,-2407,-3411,-1467,4295,1271,-778,-2478,-2442,4041,-1031,1999,973,-3647,3312,-4773,2333,-719,-4644,1092,-4944,4525,3831,4107,-1816}}, 
12                  {{9680,-3756,-5569,3402,4704,-6267,-2393,3191,-965,-5042,-6951,-6184,-8752,525,2144,6195,541,-3336,6904,-3130,-3261,-4770,-34,-1991,292,3703,4590,7406,2272,5190,-5336,-1521,4381,-1579,4700,269,-1059,-2249,-166,-2553,-1098,-6839,-4172,303,7838,256,-931,-5243,-51,267,-3432,2351,58,49,5963,253,3616,-7918,-4486,1256,-3305,-5528,-5891,3779,-4004,950,-8741,1864,-1158,871,-2899,5178,3183,-784,-7603,-984,-2407,-3411,-1467,4295,1271,-778,-2478,-2442,4041,-1031,1999,973,-3647,3312,-4773,2333,-719,-4644,1092,-4944,4525,3831,4107,-1816},{9680,-3756,-5569,3402,4704,-6267,-2393,3191,-965,-5042,-6951,-6184,-8752,525,2144,6195,541,-3336,6904,-3130,-3261,-4770,-34,-1991,292,3703,4590,7406,2272,5190,-5336,-1521,4381,-1579,4700,269,-1059,-2249,-166,-2553,-1098,-6839,-4172,303,7838,256,-931,-5243,-51,267,-3432,2351,58,49,5963,253,3616,-7918,-4486,1256,-3305,-5528,-5891,3779,-4004,950,-8741,1864,-1158,871,-2899,5178,3183,-784,-7603,-984,-2407,-3411,-1467,4295,1271,-778,-2478,-2442,4041,-1031,1999,973,-3647,3312,-4773,2333,-719,-4644,1092,-4944,4525,3831,4107,-1816}},
13                  {{9680,-3756,-5569,3402,4704,-6267,-2393,3191,-965,-5042,-6951,-6184,-8752,525,2144,6195,541,-3336,6904,-3130,-3261,-4770,-34,-1991,292,3703,4590,7406,2272,5190,-5336,-1521,4381,-1579,4700,269,-1059,-2249,-166,-2553,-1098,-6839,-4172,303,7838,256,-931,-5243,-51,267,-3432,2351,58,49,5963,253,3616,-7918,-4486,1256,-3305,-5528,-5891,3779,-4004,950,-8741,1864,-1158,871,-2899,5178,3183,-784,-7603,-984,-2407,-3411,-1467,4295,1271,-778,-2478,-2442,4041,-1031,1999,973,-3647,3312,-4773,2333,-719,-4644,1092,-4944,4525,3831,4107,-1816},{9680,-3756,-5569,3402,4704,-6267,-2393,3191,-965,-5042,-6951,-6184,-8752,525,2144,6195,541,-3336,6904,-3130,-3261,-4770,-34,-1991,292,3703,4590,7406,2272,5190,-5336,-1521,4381,-1579,4700,269,-1059,-2249,-166,-2553,-1098,-6839,-4172,303,7838,256,-931,-5243,-51,267,-3432,2351,58,49,5963,253,3616,-7918,-4486,1256,-3305,-5528,-5891,3779,-4004,950,-8741,1864,-1158,871,-2899,5178,3183,-784,-7603,-984,-2407,-3411,-1467,4295,1271,-778,-2478,-2442,4041,-1031,1999,973,-3647,3312,-4773,2333,-719,-4644,1092,-4944,4525,3831,4107,-1816}}
14              };
15          Scanner c = new Scanner(System.in) ;
16          System.out.println("請輸入beginX(<=3):");
17          int beginX = c.nextInt() ; 
18          System.out.println("請輸入overX(<=3):");
19          int overX =c.nextInt();
20          System.out.println("請輸入beginY(<=2):");
21          int beginY = c.nextInt() ; 
22          System.out.println("請輸入overY(<=2):");
23          int overY =c.nextInt();
24          System.out.println("請輸入beginZ(<=100):");
25          int beginZ = c.nextInt() ; 
26          System.out.println("請輸入overZ(<=100):");
27          int overZ =c.nextInt();
28          System.out.println("從第"+beginX+"行第"+beginY+"列第"+beginZ+"個到第"+overX+"行第"+overY+"列第"+overZ+"個的和為:");
29          for(int i=beginX-1;i<=overX-1;i++)
30           {
31               for(int j=beginY-1;j<=overY-1;j++)
32              {
33                   for(int k=beginZ-1;k<=overZ-1;k++)//類比二維陣列
34                   {
35                       sum+=cc[i][j][k];         
36              
37                   }
38              }
39               
40           }
41           System.out.println(cc[beginX-1][beginY-1][beginZ-1]+"+...+"+cc[overX-1][overY-1][overZ-1]+"="+sum);
42      }
43  }
44  

執行結果:

 

      因為這學期電腦重灌了系統所以一直沒有裝編譯軟體、一直到週末才開始準備這次的程式設計、所以說基本是為了趕作業而邊編寫的程式,還有非常多需要改進的地方,希望老師加以指點。

       這次程式設計遇到的編譯問題有很多,比如第m行第n列在寫迴圈的時候要寫第m-1行第n-1列,最後的結果才對,一開始忘記了,後來用短一點的資料自己算了一下才發現的,因為每行每列開頭的識別符號是0而不是1,很容易忘記。

錯誤:

      還有很多因為不小心而忽略的細節,自己以後也要加以改正。

      這次完成作業前提是看了林傑的程式設計後有所啟發、後期在編譯過程中有很多地方,很多地方都是在和曹磊同學一起討論、細節上共同探討和完善然後得出結果。在此表示感謝!

 

相關文章