1. 设计一个类(不妨叫做A ),实现对一维数组的指定范围内的元素相加运算。
package nsk; import java.util.Scanner;//用于java输入语句的包。 public class A { public static void main(String[]args) { 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}; int sum=0; Scanner sc = new Scanner(System.in) ;//定义对象,用到Scanner类的nextInt()方法 System.out.println("请输入start:"); int start=sc.nextInt(); System.out.println("请输入end:"); int end=sc.nextInt(); System.out.println("str为:" +start); System.out.println("end为:" +end); System.out.println("第"+start+"个元素到第"+end+"个元素相加和为"); for(int i=start;i<=end;i++)//用for循环语句运算 sum+=arr[i]; System.out.println(sum); } }
MyEclipse 6.0运算结果如图:
2. 设计一个类(不妨叫做B ),实现对二维数组的指定范围内的元素相加运算。
package nsk; import java.util.Scanner; public class B { public static void main(String[]args){ int[][]b={ { 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 }, { 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, } }; int sum=0; Scanner bb = new Scanner(System.in); System.out.println("请输入开始x:"); int beginI = bb.nextInt(); System.out.println("请输入结束x:"); int overI =bb.nextInt(); System.out.println("请输入开始y:"); int beginJ = bb.nextInt(); System.out.println("请输入结束y:"); int overJ =bb.nextInt(); System.out.println("请输入和为:"); for(int i=beginI-1;i<=overI-1;i++) { for(int j=beginJ-1;j<=overJ-1;j++) { sum+=b[i][j]; } } System.out.println(b[beginI-1][beginJ-1]+"+...+"+b[overI-1][overJ-1]+"="+sum); } }
3. 设计一个类(不妨叫做C ),实现对三维数组的指定范围内的元素相加运算。
package nsk; import java.util.Scanner; public class C { public static void main(String[]args) { int sum=0; int[][][] cc = 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,292, }, { 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, } }, { { 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, }, { 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, } }, { { 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, }, { 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, } } }; Scanner c = new Scanner(System.in) ; System.out.println("请输入开始X(<=3):"); int beginX = c.nextInt() ; System.out.println("请输入结束X(<=3):"); int overX =c.nextInt(); System.out.println("请输入开始Y(<=2):"); int beginY = c.nextInt() ; System.out.println("请输入结束Y(<=2):"); int overY =c.nextInt(); System.out.println("请输入开始Z(<=100):"); int beginZ = c.nextInt() ; System.out.println("请输入结束Z(<=100):"); int overZ =c.nextInt(); System.out.println("从第"+beginX+"行第"+beginY+"列第"+beginZ+"个到第"+overX+"行第"+overY+"列第"+overZ+"个的和为:"); for(int i=beginX-1;i<=overX-1;i++) { for(int j=beginY-1;j<=overY-1;j++) { for(int k=beginZ-1;k<=overZ-1;k++) { sum+=cc[i][j][k]; } } } System.out.println(cc[beginX-1][beginY-1][beginZ-1]+"+...+"+cc[overX-1][overY-1][overZ-1]+"="+sum); } }
小结: 首先看到题目,我难以着手的是如何将excal表格中的数据导入到MyEclipse 6.0中,自己琢磨了一会儿,觉得还是用最实在的方法--一个个手动导入吧。
其次,分析题目:对数组元素相加,这个不难,用for循环语句就可以了,但是如何在“指定范围数据内”相加就比较麻烦了,由同学提示:Scanner类可以实现从键盘读取数据。然后我也参考了其他同学运行好的代码,自己也调试了几遍。通过这次练习,我明白了编写程序就是一个不断发现问题,然后解决问题的过程,一定要多尝试,不怕错误失败,慢慢调试,就一定会有进步的。