《软件工程》第2次作业(1、个人项目实践)

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,很容易忘记。

错误:

      还有很多因为不小心而忽略的细节,自己以后也要加以改正。

      这次完成作业前提是看了林杰的编程后有所启发、后期在编译过程中有很多地方,很多地方都是在和曹磊同学一起讨论、细节上共同探讨和完善然后得出结果。在此表示感谢!

 

posted @ 2015-09-21 12:58  GEM茶度余生  阅读(263)  评论(3编辑  收藏  举报