1. 设计一个类(不妨叫做A ),实现对一维数组的指定范围内的元素相加运算。
package xxx; import java.util.Scanner; public class xkxkxkx { public static void main(String[]args) { int[] arr1={767,-4619,2209,-7862,-3195,5676,4553,-784,-2010,-3046,1566,-1562,1021,2943,-7241,3847,3222,3001,-1731,-4889,2544,-7893,-1142,7523,7559,867,-4088,2001,-2532,-4488,-8557,5088,-7906,-2104,-1911,-1048,1886,5500,-890,1386,-2080,1431,-1607,5808,-8107,4418,450,1261,-3869,-1184,418,2413,-6418,4607,4059,-444,7821,-2605,-2241,-1390,3047,-1588,8529,1116,4085,4512,4008,4242,6382,-1824,2455,-9217,6014,-2490,-7241,-845,9680,-3756,-5569,3402,4704,-6267,-2393,3191,-965,-5042,-6951,-6184,-8752,525,2144,6195,541,-3336,6904,-3130,-3261,-4770,-34,-1991,}; int sum=0; Scanner se = new Scanner(System.in) ; System.out.println("请输入x开始数:"); int start = se.nextInt() ; System.out.println("请输入x结束数:"); int end =se.nextInt(); System.out.println("x开始为:" +start); System.out.println("x结束为:" +end); for(int i=start;i<=end;i++) sum+=arr1[i]; System.out.println(sum); } }
上面是一维数组的相加。
运行结果图:
2.设计一个类(不妨叫做B ),实现对二维数组的指定范围内的元素相加运算。
package xxx2; import java.util.Scanner; public class xkxkxkx2 { public static void main(String[]args){ int[][] a={{767,-4619,2209,-7862,-3195,5676,4553,-784,-2010,-3046,1566,-1562,1021,2943,-7241,3847,3222,3001,-1731,-4889,2544,-7893,-1142,7523,7559,867,-4088,2001,-2532,-4488,-8557,5088,-7906,-2104,-1911,-1048,1886,5500,-890,1386,-2080,1431,-1607,5808,-8107,4418,450,1261,-3869,-1184,418,2413,-6418,4607,4059,-444,7821,-2605,-2241,-1390,3047,-1588,8529,1116,4085,4512,4008,4242,6382,-1824,2455,-9217,6014,-2490,-7241,-845,9680,-3756,-5569,3402,4704,-6267,-2393,3191,-965,-5042,-6951,-6184,-8752,525,2144,6195,541,-3336,6904,-3130,-3261,-4770,-34,-1991,}, {-2574,2266,2530,481,203,1289,5307,-1328,-4329,5401,6352,2410,-5994,-1825,-8731,-1917,1126,-6189,1290,2701,-1426,-2867,3269,3902,-6483,3591,-313,-53,8749,-742,8649,-276,2687,834,3887,2743,-625,-4357,-5245,-858,-1519,1635,-3765,-318,-4702,-2618,-1070,5750,743,2086,-927,9357,1359,5569,-1483,3141,-685,-1718,-5255,363,2403,1298,861,4910,-5958,6316,-4404,1294,3274,5661,3305,-5349,4768,-7717,3969,742,-1184,-246,3193,5956,-8664,2275,2207,-2697,3871,-5330,-2132,2420,3590,2387,2655,-431,3285,1501,-5325,-2364,1373,-1288,-2344,-3866,}}; int sum=0; Scanner se = new Scanner(System.in); System.out.println("输入X的开始数:"); int start1 = se.nextInt(); System.out.println("输入x的结束数:"); int end1 =se.nextInt(); System.out.println("x开始数:" +start1); System.out.println("x结束数:" +end1); System.out.println("输入y的开始数::"); int start2 = se.nextInt(); System.out.println("输入y的开始数::"); int end2 =se.nextInt(); System.out.println("y开始数:" +start2); System.out.println("y结束数:" +end2); for(int i=start1;i<=end1;i++) { for(int j=start2;j<=end2;j++) { sum+=a[i][j]; } } System.out.println(sum); } }
上面的是二维数组的源代码。
运行的结果图如下:
3. 设计一个类(不妨叫做C ),实现对三维数组的指定范围内的元素相加运算。
package xxx3; import java.util.Scanner; public class xkxkxkx3 { public static void main(String[]args) { int sum=0; int[][][] a = new int[][][] { {{767,-4619,2209,-7862,-3195,5676,4553,-784,-2010,-3046,1566,-1562,1021,2943,-7241,3847,3222,3001,-1731,-4889,2544,-7893,-1142,7523,7559,867,-4088,2001,-2532,-4488,-8557,5088,-7906,-2104,-1911,-1048,1886,5500,-890,1386,-2080,1431,-1607,5808,-8107,4418,450,1261,-3869,-1184,418,2413,-6418,4607,4059,-444,7821,-2605,-2241,-1390,3047,-1588,8529,1116,4085,4512,4008,4242,6382,-1824,2455,-9217,6014,-2490,-7241,-845,9680,-3756,-5569,3402,4704,-6267,-2393,3191,-965,-5042,-6951,-6184,-8752,525,2144,6195,541,-3336,6904,-3130,-3261,-4770,-34,-1991,}}, {{-2574,2266,2530,481,203,1289,5307,-1328,-4329,5401,6352,2410,-5994,-1825,-8731,-1917,1126,-6189,1290,2701,-1426,-2867,3269,3902,-6483,3591,-313,-53,8749,-742,8649,-276,2687,834,3887,2743,-625,-4357,-5245,-858,-1519,1635,-3765,-318,-4702,-2618,-1070,5750,743,2086,-927,9357,1359,5569,-1483,3141,-685,-1718,-5255,363,2403,1298,861,4910,-5958,6316,-4404,1294,3274,5661,3305,-5349,4768,-7717,3969,742,-1184,-246,3193,5956,-8664,2275,2207,-2697,3871,-5330,-2132,2420,3590,2387,2655,-431,3285,1501,-5325,-2364,1373,-1288,-2344,-3866,}}, {{1939,-2596,-858,-2040,4980,5915,-744,2107,-2565,-3527,24,2430,1942,1246,-8103,-3588,1762,-1975,-4052,-2843,781,230,3877,-6604,3513,6904,-1993,-516,-4908,2714,1288,-4599,1281,-2047,-2705,-3246,8467,-1460,226,-5227,-6338,-3984,-5094,6312,4502,-2448,-3217,-6147,-752,5938,1933,-4498,6788,-407,-2946,4987,-3366,-7439,-530,1666,-1079,4508,-2868,518,-5588,729,-1388,-7851,-5506,2141,3110,2790,1178,5286,451,-5705,7100,2334,1084,-5854,-4476,1077,-3918,5448,334,-5068,7145,-3070,6044,-7056,6598,-7050,-2211,-221,-2611,-6513,1332,-4269,3093,4146}} }; Scanner se = new Scanner(System.in) ; System.out.println("输入X的开始数::"); int start1 = se.nextInt() ; System.out.println("请输入x的结束数:"); int end1 =se.nextInt(); System.out.println("x开始数为:" +start1); System.out.println("x结束数为:" +end1); System.out.println("输入y的开始数::"); int start2 = se.nextInt() ; System.out.println("请输入y的结束数:"); int end2 =se.nextInt(); System.out.println("y开始数为:" +start2); System.out.println("y结束数为:" +end2); System.out.println("请输入z的开始数:"); int start3 = se.nextInt() ; System.out.println("请输入z的结束数:"); int end3 =se.nextInt(); System.out.println("z的开始数为:" +start3); System.out.println("z的结束数为:" +end3); for(int i=start1;i<=end1;i++) { for(int j=start2;j<=end2;j++) { for(int q=start3;q<=end3;q++) { sum+=a[i][j][q]; } } } System.out.println(sum); } }
上面的是三维数组的源代码。
运行结果图:
代码编写经历和小结:
刚开始看到老师布置作业的时候 整个人都傻了 这是什么鬼? 本来打算放弃了 然后通过群里面老师推荐的2位同学的作业结果 进行了借鉴,同时听取了老师的讲解,但是自己才疏学浅并不能完成改造。
进过一个晚上的不懈努力终于做出了个样子,希望老师下手轻点。
刚开始做的时候代码什么的都不太清楚,所以请教了林杰同学,借鉴了他的代码同时听他给我讲解了代码的原理。
在自己做的过程中有多多少少的问题 比如说如何从Excel中提取数据,提取数据是从张振渊同学的随笔中找到的附上一个传送门给需要的童鞋(http://www.cnblogs.com/Peristaltic/p/4805497.html)。
然后对代码的看法 其实就是一种逻辑思维 一维 二维平面 三维立体 X,Y,Z 把它表达出来 在通过老师的数据导入 从而得出结果。
在这个编写的过程中 自己明白了很多以前不懂的操作 也有了对代码的理解。
最后非常谢谢林杰同学和老师,助教的指导。