标签:
1.设计一个类(不妨叫做A),实现对一维数组的指定范围内的元素相加运算。
1 package ggg; 2 import java.util.Scanner; 3 public class A { 4 public static void main(String[]args) 5 { 6 int[] aa={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,292 7 }; 8 int sum=0; 9 Scanner a = new Scanner(System.in); 10 System.out.println("开始:"); 11 int begin = a.nextInt() ; 12 System.out.println("结束:"); 13 int over =a.nextInt(); 14 System.out.println("和为:"); 15 16 for(int i=begin-1;i<=over-1;i++) 17 sum+=aa[i]; 18 System.out.println(aa[begin-1]+"+...+"+aa[over-1]+"="+sum); 19 20 } 21 }
注:此时按照内行数从100~200行。
运行结果:
验证:
2. 设计一个类(不妨叫做B ),实现对二维数组的指定范围内的元素相加运算。
1 package ggg; 2 import java.util.Scanner; 3 public class B { 4 public static void main(String[]args){ 5 int[][]b={ 6 {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,292 7 }, 8 {7309,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,1900, 9 } 10 }; 11 int sum=0; 12 Scanner bb = new Scanner(System.in); 13 System.out.println("开始x:"); 14 int beginI = bb.nextInt(); 15 System.out.println("结束x:"); 16 int overI =bb.nextInt(); 17 System.out.println("开始y:"); 18 int beginJ = bb.nextInt(); 19 System.out.println("结束y:"); 20 int overJ =bb.nextInt(); 21 System.out.println("和为:"); 22 23 for(int i=beginI-1;i<=overI-1;i++) 24 { 25 for(int j=beginJ-1;j<=overJ-1;j++) 26 { 27 sum+=b[i][j]; 28 } 29 } 30 System.out.println(b[beginI-1][beginJ-1]+"+...+"+b[overI-1][overJ-1]+"="+sum); 31 } 32 }
注:此时按照A的100~200行,B的100~200行。
运行结果:
3. 设计一个类(不妨叫做C ),实现对三维数组的指定范围内的元素相加运算。
1 package ggg; 2 import java.util.Scanner; 3 4 public class C { 5 6 7 public static void main(String[]args) 8 { 9 int sum=0; 10 int[][][] cc = new int[][][] 11 { 12 {{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,292, 13 }, 14 {7309,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,1900, 15 } 16 }, 17 { 18 {7309,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,1900, 19 }, 20 {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,3059, 21 } 22 }, 23 { 24 {7309,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,1900, 25 }, 26 {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,3059, 27 } 28 } 29 }; 30 Scanner c = new Scanner(System.in) ; 31 System.out.println("请输入beginX(<=3):"); 32 int beginX = c.nextInt() ; 33 System.out.println("请输入overX(<=3):"); 34 int overX =c.nextInt(); 35 System.out.println("请输入beginY(<=2):"); 36 int beginY = c.nextInt() ; 37 System.out.println("请输入overY(<=2):"); 38 int overY =c.nextInt(); 39 System.out.println("请输入beginZ(<=100):"); 40 int beginZ = c.nextInt() ; 41 System.out.println("请输入overZ(<=100):"); 42 int overZ =c.nextInt(); 43 System.out.println("从第"+beginX+"行第"+beginY+"列第"+beginZ+"个到第"+overX+"行第"+overY+"列第"+overZ+"个的和为:"); 44 for(int i=beginX-1;i<=overX-1;i++) 45 { 46 for(int j=beginY-1;j<=overY-1;j++) 47 { 48 for(int k=beginZ-1;k<=overZ-1;k++) 49 { 50 sum+=cc[i][j][k]; 51 52 } 53 } 54 55 } 56 System.out.println(cc[beginX-1][beginY-1][beginZ-1]+"+...+"+cc[overX-1][overY-1][overZ-1]+"="+sum); 57 } 58 }
运行结果:
小结:
通过这次作业,我发现通过代码实现功能的过程是一个由简单到复杂的过程,我在编写的过程中先实现了一维数组指定范围内的相加减,通过类比,自然地实现了多维范围的相加减。
在代码中主要用到了从键盘输入数据的Scanner类的nextInt()方法和循环for语句和以开始结束做为下标参数。
其次就是想跟老师您说明一下我的基础不是很扎实,如果靠我一个人单独去完成这次作业还是有点难度的,所以这次完成作业前提是借鉴了一下林杰的编程后有所启发、后期在编译过程中有很多地方都是在和王威同学一起讨论、细节上共同探讨和完善然后得出结果。上次作业老师说了只要动手做就不会没有分数,你要完全copy就是0分,本人身同感受,最后希望跟老师后面能够多学点东西,反正现在自己可以自己动手做些东西,总比原本什么事都不做的好,在此表示感谢!
标签:
原文地址:http://www.cnblogs.com/caolei1108/p/4826883.html