第二次作业(个人项目实践)

1. 设计一个类(不妨叫做A ),实现对一维数组(a[100])的指定范围内的元素相加运算。 

 1 package aaa;
 2 import java.util.Scanner;//用于java输入语句的包。
 3 
 4 public class A {
 5      public static void main(String[]args)
 6        {   
 7          int[] arr={-2574,-4010,8243,771,2447,-5197,2556,8044,3314,3617,6065,-2817,3131,6318,2186,-113,629,-2582,-37,-1520,164,2055,-5936,5912,1717,5988,4781,5757,892,-4394,8034,2213,-1080,-2080,5364,106,2657,566,3940,-5116,4583,1806,6555,2621,-7197,528,1626,18,1049,6243,3198,4397,-1325,9087,936,-6291,662,-178,135,-3473,-2385,-165,1713,-7949,-4234,1138,2212,104,6968,-3632,3801,1137,-1296,-1215,4272,6223,-5922,-7723,7044,-2938,-8180,1356,1159,-4022,-3713,1158,-8715,-4081,-2541,-2555,-2284,461,940,6604,-3631,3802,-2037,-4354,-1213,767};
 8          int sum=0;
 9          Scanner sc = new Scanner(System.in) ;//定义Scanner类的对象sc,接收来自键盘的数据
10          System.out.println("x轴开始:");
11          int start=sc.nextInt();
12          System.out.println("x轴结束:");
13          int end=sc.nextInt();
14          System.out.println("str为:" +start);
15          System.out.println("end为:" +end);
16          System.out.println("第"+start+"个元素到第"+end+"个元素相加和为");
17          for(int i=start;i<=end;i++)
18          sum+=arr[i];                             
19          System.out.println(sum); 
20        }     
21 }
运行结果:

第二次作业(个人项目实践)_第1张图片

 

2. 设计一个类(不妨叫做B ),实现对二维数组(a[N][M])的指定范围内的元素相加运算。 

 1 package aaa;
 2 import java.util.Scanner;
 3 
 4 public class B 
 5 {
 6  public static void main(String[]args)
 7  {
 8    int[][] 
 9   a={
10     {-2574,-4010,8243,771,2447,-5197,2556,8044,3314,3617,6065,-2817,3131,6318,2186,-113,629,-2582,-37,-1520,164,2055,-5936,5912,1717,5988,4781,5757,892,-4394,8034,2213,-1080,-2080,5364,106,2657,566,3940,-5116,4583,1806,6555,2621,-7197,528,1626,18,1049,6243,3198,4397,-1325,9087,936,-6291,662,-178,135,-3473,-2385,-165,1713,-7949,-4234,1138,2212,104,6968,-3632,3801,1137,-1296,-1215,4272,6223,-5922,-7723,7044,-2938,-8180,1356,1159,-4022,-3713,1158,-8715,-4081,-2541,-2555,-2284,461,940,6604,-3631,3802,-2037,-4354,-1213,767},
11     {2740,-4182,-5632,-2966,-1953,567,-8570,1046,2211,1572,-2503,-1899,3183,-6187,3330,3492,-464,-2104,316,8136,470,50,466,-1424,5809,2131,6418,-3018,6002,-8379,1433,1144,2124,1624,-602,-5518,5872,870,-5175,-3961,-427,-6284,2850,481,6175,141,-766,-1897,-748,-4248,366,4823,3003,1778,3256,2182,2253,5076,5540,-2650,2451,-1875,5482,-6881,-329,-969,-8032,-2093,612,1524,-5492,5758,-7401,-5039,3241,6338,3581,4321,-1072,4942,2131,210,-7045,-7514,7450,-1142,-2666,-4485,-639,2121,-5298,-3805,-1686,-2520,-1680,2321,-4617,-1961,2076,7309
12   }
13 }
14   int sum=0;
15   Scanner sc = new Scanner(System.in); //定义Scanner类的对象sc,接收来自键盘的数据
16   System.out.println("x轴开始:"); //将二维数组看作是平面直角坐标系,有x,y两个坐标
17   int startX = sc.nextInt(); 
18   System.out.println("x轴结束:");//1-2
19   int endX =sc.nextInt();
20   System.out.println("y轴开始:");//1-100
21   int startY = sc.nextInt(); 
22   System.out.println("y轴结束:");
23   int endY =sc.nextInt();
24   System.out.println("指定的范围是");
25   System.out.println("从("+startX+","+startY+")到"+"("+endX+","+endY+")的和为");
26   for(int i=startX-1;i<=endX-1;i++)
27   {
28       for(int j=startY-1;j<=endY-1;j++)
29         {
30         sum+=a[i][j];
31         }
32   }
33   System.out.println(sum);  
34  }
35 }

运行结果:

第二次作业(个人项目实践)_第2张图片

3. 设计一个类(不妨叫做C ),实现对三维数组(a[N][M][K])的指定范围内的元素相加运算。

 1 package aaa;
 2 import java.util.Scanner;
 3 
 4 public class C {
 5     public static void main(String[]args)
 6     {
 7         int sum=0;
 8          int[][][] a = new int[][][]
 9             {
10                 {{-2574,-4010,8243,771,2447,-5197,2556,8044,3314,3617,6065,-2817,3131,6318,2186,-113,629,-2582,-37,-1520,164,2055,-5936,5912,1717,5988,4781,5757,892,-4394,8034,2213,-1080,-2080,5364,106,2657,566,3940,-5116,4583,1806,6555,2621,-7197,528,1626,18,1049,6243,3198,4397,-1325,9087,936,-6291,662,-178,135,-3473,-2385,-165,1713,-7949,-4234,1138,2212,104,6968,-3632,3801,1137,-1296,-1215,4272,6223,-5922,-7723,7044,-2938,-8180,1356,1159,-4022,-3713,1158,-8715,-4081,-2541,-2555,-2284,461,940,6604,-3631,3802,-2037,-4354,-1213,767}}, 
11                 {{2740,-4182,-5632,-2966,-1953,567,-8570,1046,2211,1572,-2503,-1899,3183,-6187,3330,3492,-464,-2104,316,8136,470,50,466,-1424,5809,2131,6418,-3018,6002,-8379,1433,1144,2124,1624,-602,-5518,5872,870,-5175,-3961,-427,-6284,2850,481,6175,141,-766,-1897,-748,-4248,366,4823,3003,1778,3256,2182,2253,5076,5540,-2650,2451,-1875,5482,-6881,-329,-969,-8032,-2093,612,1524,-5492,5758,-7401,-5039,3241,6338,3581,4321,-1072,4942,2131,210,-7045,-7514,7450,-1142,-2666,-4485,-639,2121,-5298,-3805,-1686,-2520,-1680,2321,-4617,-1961,2076,7309}},
12                 {{302,-7944,-6551,6648,-884,-6332,1188,-248,3902,-2171,-3782,-3510,6203,-636,-4605,4497,3187,-6278,1198,-7963,7647,-3022,-8267,7995,-1998,-4171,730,-8117,-1706,7601,-2454,-6161,-1810,243,-2841,-5377,2985,2328,177,5622,3596,3057,2830,-1151,6566,2254,6972,-3060,4063,367,84,3156,3880,-1530,5272,1385,-565,-1736,1267,-3863,-229,2072,2423,-7449,9555,2844,-1278,-3613,-919,4153,-4232,4032,-5732,199,4792,2498,-3344,-4193,2650,-579,3416,-3575,1822,-6936,-2661,-1873,414,2314,6844,7613,-7844,6680,5974,2208,-3597,6302,4396,-141,-1864,1939}}
13             };
14         Scanner sc = new Scanner(System.in) ; //定义Scanner类的对象sc,接收来自键盘的数据
15         System.out.println("x轴开始:");//将三维数组看作是空间直角坐标系,有x,y,z三个坐标
16      int startX = sc.nextInt() ; 
17         System.out.println("x轴结束:");x取值0-2
18         int endX =sc.nextInt();        
19         System.out.println("y轴开始:");
20         int startY = sc.nextInt() ; 
21         System.out.println("y轴结束:");
22         int endY =sc.nextInt();
23         System.out.println("z轴开始:");
24         int startZ = sc.nextInt() ; 
25         System.out.println("z轴结束:");
26         int endZ =sc.nextInt();
27        
28         for(int i=startX;i<=endX;i++)
29          {
30              for(int j=startY;j<=endY;j++)
31             {
32                  for(int q=startZ;q<=endZ;q++)
33                  {
34                      sum+=a[i][j][q];         
35                  }
36             }    
37          }
38          System.out.println("从("+startX+","+startY+","+startZ+")到"+"("+endX+","+endY+","+endZ+")的和为");
39          System.out.println(sum);
40     }
41 }

运行结果:

 第二次作业(个人项目实践)_第3张图片

    说实话,看到这个作业的时候真的是毫无头绪,就我所理解的,是要我们编写一个程序将老师给的excel里的数据导入到myeclipse中,并且将数据放到数组中,运用类来实现对一维数组、二维数据以及三维数组的元素相加运算,可是如何将excel中的那些数据导到myeclipse中就已经难倒了我。后来,我看了张振渊同学和林杰的博客,发现他们是通过手动导入的方式将数据导入,于是我看了他们的方法:

http://jingyan.baidu.com/article/c85b7a642ceff6003bac95f0.html,

http://jingyan.baidu.com/article/a24b33cd5730bc19fe002b3e.html 

分别导出一维数组、二维数组以及三维数组要用的数据。

     虽然导出了数据,但是新的问题又出现了,要实现的是指定范围内的元素相加运算,那肯定要手动输入需要的范围,通过翻阅以前的Java书,我发现Scanner类可以实现从键盘读取数据,书上列出了Scanner类的常用方法,如Scanner(String source)可以构造一个新的Scanner(从指定字符串扫描数据)、int nextInt()(扫描读取下一个数据,该数据被标识为int值)等等。教材中列举了一个关于计算总金额的例子,

 1 import java.until.Scanner;
 2 
 3  public class TotalMon
 4 
 5  {
 6 
 7   public static void main(String[] args) 
 8 
 9   double totalMon,UnitPrice;
10 
11   int num;
12 
13   Scanner sc=new Scanner(System.in);
14 
15   String str=sc.nextLine();
16 
17   Scanner scan=new Scanner(str);
18 
19   scan.useDelimiter(“/”);
20 
21   UnitPrice=scan.nextDouble();
22 
23   num=scan.nextInt();
24 
25   totalMon=UnitPrice*num;
26 
27   System.out.println(“总金额为:”+totalMon);
28 
29  }

      后来就尝试自己编写这次的代码,我是把测试数据放在代码中,比较繁琐,也有尝试用工具类和测试类才编写,但是还没有成功。我仔细看了其他人写的代码以及老师给的评论,都是把测试数据放在代码中,老师也一再强调代码优化问题,最好能从csv中读取excel的数据。通过这次实验,我收获很多,明白了编程要多钻研、多尝试,同时代码规范也很重要,这次写随笔我有注意自己文章的规范性。

 

你可能感兴趣的:(第二次作业(个人项目实践))