码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-09-19 22:44:59      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:

 1 package aaa;
 2 import java.util.Scanner;//用于java输入语句的包。
 3 
 4 public class A {
 5     
 6     public static void main(String[]args)
 7     {   
 8         int[] arr1={-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};//因为实现不用Java写程序导入数据,所以这里直接将数据copy过来了。
 9         int sum=0;
10 Scanner se = new Scanner(System.in) ;//因为要从键盘输入你想要取的开始和结束的数,所以这里用到Scanner类的nextInt()方法。
11 System.out.println("请输入start:"); 12 int start = se.nextInt() ; 13 System.out.println("请输入end:"); 14 int end =se.nextInt(); 15 System.out.println("start为:" +start); 16 System.out.println("end为:" +end);
17 for(int i=start;i<=end;i++)/*这边用start/end做下标(start/end是你在上边键盘输入的),再用for语 18 句循环和sum+=arr1[]进行累加可以表示出你要取的范围,例如start:2,end=9, 19 就可以输出上述一维数组中下标为2到9的数的和。*/ 20 sum+=arr1[i]; 21 System.out.println(sum); 22 } 23 24 }
arr1指定范围内相加的两个结果:

1. 

技术分享

验证:

技术分享

2.

技术分享

验证:

技术分享

 1 package aaa;
 2 
 3 import java.util.Scanner;
 4 
 5 public class B {
 6     
 7     public static void main(String[]args){
 8         int[][] a={{-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},{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}};
 9         int sum=0;
10         Scanner se = new Scanner(System.in);
11         System.out.println("输入start:");
12         int start = se.nextInt(); 
13         System.out.println("输入end:");
14         int end =se.nextInt();
15         System.out.println("start:" +start);
16         System.out.println("end:" +end);
17         for(int i=0;i<2;i++)
18         {
19             for(int j=start;j<=end;j++)//类比一维数组可以得出。
20             {
21             sum+=a[i][j];
22             }
23         }
24         System.out.println(sum);  
25     }
26 
27 }
arr2指定范围内相加的两个结果:

1.技术分享验证:技术分享2.技术分享验证:技术分享

 1 package aaa;
 2 
 3 import java.util.Scanner;
 4 
 5 public class C {
 6 
 7     
 8     public static void main(String[]args)
 9     {
10         int sum=0;
11          int[][][] a = new int[][][]
12             {
13                 {{-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}}, 
14                 {{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}},
15                 {{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}}
16             };//这边的三位数组定义方式是宝玉老师给的建议,看起来比较抽象,因为是y=0的三维数组。
17         Scanner se = new Scanner(System.in) ;
18         System.out.println("请输入start:");
19         int start = se.nextInt() ; 
20         System.out.println("请输入end:");
21         int end =se.nextInt();
22         System.out.println("start为:" +start);
23         System.out.println("end为:" +end);
24         for(int i=0;i<3;i++)
25          {
26              for(int j=0;j<1;j++)
27             {
28                  for(int q=start;q<=end;q++)//类比二维数组可以得出。
29                  {
30                      sum+=a[i][j][q];         
31             
32                  }
33             }
34              
35          }
36          System.out.println(sum);
37     }
38 }
arr2指定范围内相加的两个结果:

1.技术分享验证:技术分享2.技术分享验证:技术分享

代码书写经历和小结:

       一开始在看老师给的要求时,我以为要求就是实现一二三维数组从1~100的相加,后来觉得太简单了就去问老师,才知道对题目的理解有错误,而且老师给了建议让我把主函数封装成方法来体现出自己写法中类的封装性(可惜我没有实现,基础知识没打好,有点没有头绪),还让我设定start/end参数做下标来实现指定范围内的相加(这个应该算做到了),我是看到start/end才知道题目的原意是1~100任意范围内的相加,就是说start和end的数值由自己指定来实现范围内相加。

      我在做数据的导入时没有按照老师的初衷(编写程序读取Excel数据)来完成要求,而是参考张振渊同学的博客的随笔(传送门)来手动导入数据的。随后我在初始化数组的时候也发现了很多问题1.老师在ppt上定义数组的方式和我的方式不同(后来通过查阅java书籍知道java语言支持两种语法格式来定义数组1.type[] arrayname;2.type arrayname[];而且因为第一种格式有更好的语意和可读性所以通常推荐使用第一种,从中也了解到了静态初始化和动态初始化的区别和语法格式。然后,我很轻易地初始化了一维数组,但是在初始化二维数组的时候,我脑海中能想到二维数组的草图应该是老师ppt上的那一张表格(因为导入的都是100可是我自然就以为应该是一张以x,y为轴的100X100的表格,后来画了几遍草图,才意识到自己错了,它只是2X100的矩阵),后面的三维语句在画草图的时候感觉非常的抽象,后来发现老师给了ABC三组数据,怎么写都不能构成三维数组,在群里宝玉老师的建议下,写了一个y=0的三维数组(感觉看着很别扭的三位数组)。后来的for语句的改写也在振渊同学的帮助下经过了多次的完善,输出结果也在草稿上做出多次验证。这边还想说下我在写的过程中并没有先导入大量数据,而是先从简单的数据例如一维数组{1,2,3},二维数组{{1,2,3},{4,5,6}}开始。发现自己的代码能够运行成功了再导入老师给的大量数据。实现了一个从简单到复杂的过程。而且我也发现你如果写出了一维数组的类,通过类比来写出多维数组的类还是比较容易的。

     书写3个类的过程中我用到了Scanner类的nextInt()方法,for语句循环,嵌套for语句循环,数组的静态定义,etc...。关于类的封装性,多态性,继承性,还有关于方法的调用以及工具类,测试类的有关知识,我还会在以后慢慢查漏补缺。

     这次完成作业的过程中有很多地方,都是张振渊同学提出草图,让我来具体实现,细节上共同讨论和完善然后得出结果。在此表示感谢!


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

标签:

原文地址:http://www.cnblogs.com/linlinlin/p/4822361.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!