标签:
package aaa; import java.util.Scanner;//用于java输入语句的包。 public class A { public static void main(String[]args) { 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};//老师的初衷是让我们写代码读取excel的数据,但是我实现不了,这里直接将数据copy过来了。 int sum=0; Scanner se = new Scanner(System.in) ;//因为要从键盘输入你想要取的开始和结束的数,所以这里用到Scanner类的nextInt()方法。 System.out.println("请输入start:"); int start = se.nextInt() ; System.out.println("请输入end:"); int end =se.nextInt(); System.out.println("start为:" +start); System.out.println("end为:" +end); for(int i=start;i<=end;i++)/*这边用start/end做下标(start/end是你在上边键盘输入的),再用for语 句循环和sum+=arr1[]进行累加可以表示出你要取的范围,例如start:2,end=9, 就可以输出上述一维数组中下标为2到9的数的和。*/ sum+=arr1[i]; System.out.println(sum); } }
arr1指定范围内相加的两个结果:1. 验证:2. 验证:
package aaa; import java.util.Scanner; public class B { public static void main(String[]args){ 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}}; int sum=0; Scanner se = new Scanner(System.in); System.out.println("输入start:"); int start = se.nextInt(); System.out.println("输入end:"); int end =se.nextInt(); System.out.println("start:" +start); System.out.println("end:" +end); for(int i=0;i<2;i++) { for(int j=start;j<=end;j++)//类比一维数组可以得出。 { sum+=a[i][j]; } } System.out.println(sum); } }
arr2指定范围内相加的两个结果:1.
验证:2.验证:
package aaa; import java.util.Scanner; public class C { public static void main(String[]args) { int sum=0; int[][][] a = new int[][][] { {{-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}}, {{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}} };//这边的三位数组定义是宝玉老师给的建议,看起来比较抽象,因为是y=0的三位数组。 Scanner se = new Scanner(System.in) ; System.out.println("请输入start:"); int start = se.nextInt() ; System.out.println("请输入end:"); int end =se.nextInt(); System.out.println("start为:" +start); System.out.println("end为:" +end); for(int i=0;i<3;i++) { for(int j=0;j<1;j++) { for(int q=start;q<=end;q++)//类比二维数组可以得出。 { sum+=a[i][j][q]; } } } System.out.println(sum); } }
arr3指定范围内相加的两个结果:1.
代码书写经历和小结:
一开始在看老师给的要求时,我以为要求就是实现一二三维数组从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...。关于类的封装性,多态性,继承性,还有关于方法的调用以及工具类,测试类的有关知识,我还会在以后慢慢查漏补缺。
这次完成作业的过程中有很多地方,都是张振渊同学提出草图,让我来具体实现,细节上共同讨论和完善然后得出结果。在此表示感谢!
标签:
原文地址:http://www.cnblogs.com/linlinlin/p/4821994.html