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

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

 1 package zwei;
 2 import java.util.Scanner;//因为等下要从键盘输入数据
 3 public class A {
 4 public static void main(String[]args)
 5 {
 6 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};
 7 int sum=0;//用于统计和
 8 Scanner se = new Scanner(System.in);            
 9 System.out.println("从第几个开始加?:");
10 int start = se.nextInt() ;
11 System.out.println("加到第几个为止?:");
12 int end =se.nextInt();
13 System.out.println("指定范围是");
14 System.out.println("第"+start+"个元素到第"+end+"个元素");
15 for(int i=start-1;i<=end-1;i++)//我希望在编程之外可以直观一点,不要整天从零计数
16 sum+=arr1[i];//于此处循环
17 System.out.println("相加运算:");
18 System.out.println(sum);
19 }
20 }

以上类即可实现对一维数组的指定范围内的元素相加。

运行结果图:

  

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

 1 package zwei;
 2 import java.util.Scanner;
 3 public class B {
 4 public static void main(String[]args){
 5 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}};
 6 int sum=0;
 7 Scanner se = new Scanner(System.in);
 8 System.out.println("从第几行开始?");//由于给出的数据原因,一共两行
 9 int startH = se.nextInt(); 
10 System.out.println("到第几行为止?");
11 int endH =se.nextInt();
12 System.out.println("从第几列开始?");//最多100列
13 int startL = se.nextInt(); 
14 System.out.println("到第几列为止?");
15 int endL =se.nextInt();
16 System.out.println("指定的范围是");
17 System.out.println("第"+startH+"行第"+startL+"列 到 第"+endH+"行第"+endL+"列");
18 for(int i=startH-1;i<=endH-1;i++)
19 {
20     for(int j=startL-1;j<=endL-1;j++)
21     {
22     sum+=a[i][j];
23     }
24 }
25 System.out.println(sum);  
26 }
27 }

 以上类即可实现对二维数组的指定范围内的元素相加。

运行结果图:

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

 1 package zwei;
 2 import java.util.Scanner;
 3 public class C {
 4 public static void main(String[]args)
 5 {
 6 int sum=0;
 7 int[][][] a = new int[][][]
 8 {
 9 {{-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}},
10 {{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}},
11 {{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}}
12 };//这边的三位数组定义是宝玉老师给的建议,看起来比较抽象,因为是y=0的三维数组。
13 Scanner se = new Scanner(System.in) ;
14 System.out.println("横坐标从几开始?");//1~3
15 int startH = se.nextInt() ;
16 System.out.println("横坐标到几为止?");
17 int endH =se.nextInt();
18 System.out.println("纵坐标从几开始?");//1~100
19 int startZ = se.nextInt() ;
20 System.out.println("纵坐标到几为止?");
21 int endZ =se.nextInt();
22 System.out.println("垂直轴从几开始?");//只有1
23 int startC = se.nextInt() ;
24 System.out.println("垂直轴到几为止?");
25 int endC =se.nextInt();
26 
27 for(int i=startH-1;i<=endH-1;i++)
28 {
29 for(int j=startC-1;j<=endC-1;j++)
30 {
31 for(int q=startZ-1;q<=endZ-1;q++)
32 {
33 sum+=a[i][j][q];
34 }
35 }
36 }
37 System.out.println(sum);
38 }
39 }

 

 以上类即可实现对三维数组的指定范围内的元素相加。

运行结果图:

 4.总结:

     基本是一个赶工之作,还有非常多需要改进的地方,比如输入数据过大的时候程序并不能判断,只会丢出个错误结果,没有数据溢出的检测机制。但至少很多由于给出数组的原因被定死在0的变量,还是按照可自行选择的方式给出了范围的选择,即使以后给出的数组范围扩充得再大,也可以将这次编程的成果直接套用了,自认为普适性还是蛮高的。编程主要是for语句的循环嵌套,一、二、三维数组的初始化还有Scanner类的nextInt()方法,本来最难的部分——编程读取excel,被我用word的操作技巧给避开了。为了完成下一次的作业的话,要马上去复习类的封装继承了。

      在程序上出现错误时,通常都会先删除无关与必定正确的代码,专心于剩下的部分寻找出错点,也常常会绕一个大圈子回到原点,最后删除多余的部分结局豁然开朗

      还有一个比较印象深刻的问题,在初次尝试书写三维数组的时候,即是编译器没有给出任何警告和错误,在MyEclipse中也是无论怎样调试都无法输出结果,感觉不知所措,惊呼三维数组不可战胜!睡了一觉起来才发现,是运行方式没有选择java应用程序,用了一个类似临时服务器的方法运行。之后的过程就非常的顺畅了,也在不长的时间内理解了三维数组的构造。当然也少不了助教在QQ群里对我提出问题的解答,最后的三位数组初始化形式也是在助教老师的点拨之下想出来的。

      这次完成作业的过程中有很多地方,都是在和林杰同学提出一些设想后,由他来完成初步测试版本,细节上共同讨论和完善然后得出结果。在此表示感谢!

posted @ 2015-09-19 22:12  zzy_01  阅读(673)  评论(6编辑  收藏  举报