package abc; import java.util.*;//导入java包 public class add { public static void main(String[] args) {int sum=0; int[] ss={-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};//插入一组数据 Scanner sc = new Scanner(System.in) ;//采用Scanner类实例化出一个对象 System.out.println("请输入m:"); int st1 = sc.nextInt() ; //调用sc对象采用nextInt方法获取数据 System.out.println("请输入n:"); int st2 = sc.nextInt() ; //同上 for(int i=st1;i<=st2;i++)//运用for语句,上线i,实现两数的相加 { sum=sum+ss[i];//具体到要取得范围 } System.out.println("m+n=" +sum); } }
add测试:
package abc; import java.util.*; public class add2 { public static void main(String[]args){ int sum=0; 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}}; Scanner sc = new Scanner(System.in); System.out.println("输入x1:"); int st1= sc.nextInt(); int x1=st1; System.out.println("输入x2:"); int en1 =sc.nextInt(); int x2=en1; System.out.println("x1:" +x1); System.out.println("x2:" +x2); System.out.println("输入y1:"); int st2 = sc.nextInt(); int y1=st2; System.out.println("输入y2:"); int en2 =sc.nextInt(); int y2=en2; System.out.println("y1:" +y1); System.out.println("y2:" +y2); for(int j=st1;j<=en1;j++) { for(int i=st2;i<=en2;i++) { sum+=a[j][i]; } } System.out.println(sum); } }
add2测试:
package abc; import java.util.Scanner; public class add3 { 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}} }; Scanner sc = new Scanner(System.in) ; System.out.println("请输入x1:"); int sta1 = sc.nextInt() ; int x1= sta1; System.out.println("请输入x2:"); int en1 =sc.nextInt(); int x2= en1; System.out.println("x1为:" +x1); System.out.println("x2为:" +x2); System.out.println("请输入y1:"); int sta2 = sc.nextInt() ; int y1=sta2; System.out.println("请输入y2:"); int en2 =sc.nextInt(); int y2=en2; System.out.println("y1为:" +y1); System.out.println("y2为:" +y2); System.out.println("请输入z1:"); int sta3 = sc.nextInt() ; int z1=sta3; System.out.println("请输入z2:"); int en3 =sc.nextInt(); int z2=en3; System.out.println("z1为:" +z1); System.out.println("z2为:" +z2); for(int i=sta1;i<=en1;i++) { for(int j=sta2;j<=en2;j++) { for(int m=sta3;m<=en3;m++) { sum+=a[i][j][m]; } } } System.out.println(sum); } }
个人小结:
起初在做的时候就是简单的实现了100个数之间的相加后来看了老师的要求之后就在想,能不能用到类的封装性来做的呢,起初的想法是在建一个工具类和一个测试类,在做测试类的时候就有很多的问题,首先是在实现两数相加的时候采用的是直接从数组里调用两个数想加,而且第一个数是从arr[0]开始的,在对最后一个数实行控制的时候我是这样想的,可不可以用到i来做限制条件呢(也就是说假如我想实现0到3相加的时候我只要把i控制在2就可以了),到时后来做的过程中发现能不能实现也对第一数也进行控制呢(也就是用到i和j两个数),i控制上线,而j控制下线,同时在控制他们大小同时就实现了对他们范围的控制,查阅书籍之后用到了Scanner类中的nextInt()的方法可以实现这个问题,解决了数组中的任意两个数据的相加。 还有一个头痛的问题就是在做二维的时候,不知道怎么实现相加,后来进过老师的讲述之后,就像一维的那样arr[][],接着就是三维arr[][][],在进行数据相加的时候,实现了一个从简单到复杂的过程,在初始化一维数组的了解到了静态初始化和动态初始化的区别和语法格式。之前的想法是从excel里导入数组的,后来百度之后还是没有头绪。又想了能不能从txt文件里导入数据呢,试了几次还是失败了。 这次的算法让我受益颇多,要想学好只有不断的学习寻求进步,同时明白了算法的道路艰难。