第二次作业:个人项目实践
1. 设计一个类(不妨叫做A ),实现对一维数组的指定范围内的元素相加运算。
1 import java.util.Scanner; 2 public class A { 3 public static void main(String[] args) { 4 int[] aa = { -2574, -4010, 8243, 771, 2447, -5197, 2556, 8044, 3314, 5 3617, 6065, -2817, 3131, 6318, 2186, -113, 629, -2582, -37, 6 -1520, 164, 2055, -5936, 5912, 1717, 5988, 4781, 5757, 892, 7 -4394, 8034, 2213, -1080, -2080, 5364, 106, 2657, 566, 3940, 8 -5116, 4583, 1806, 6555, 2621, -7197, 528, 1626, 18, 1049, 9 6243, 3198, 4397, -1325, 9087, 936, -6291, 662, -178, 135, 10 -3473, -2385, -165, 1713, -7949, -4234, 1138, 2212, 104, 6968, 11 -3632, 3801, 1137, -1296, -1215, 4272, 6223, -5922, -7723, 12 7044, -2938, -8180, 1356, 1159, -4022, -3713, 1158, -8715, 13 -4081, -2541, -2555, -2284, 461, 940, 6604, -3631, 3802, -2037, 14 -4354, -1213, 767 }; 15 int sum = 0; 16 Scanner a = new Scanner(System.in); 17 System.out.println("startsum(<=100):"); 18 int start = a.nextInt(); 19 System.out.println("endsum(<=100):"); 20 int end = a.nextInt(); 21 System.out.println("startsum" + start + "endsum" + end + "sum:"); 22 23 for (int i = start - 1; i < end; i++) 24 sum += aa[i]; 25 System.out.println(aa[start - 1] + "+...+" + aa[end - 1] + "=" + sum); 26 27 } 28 }
运行结果如图:
2.设计一个类(不妨叫做B ),实现对二维数组的指定范围内的元素相加运算。
1 import java.util.Scanner; 2 public class B{ 3 public static void main(String[] args) { 4 int[][] b = { 5 { -2574, -4010, 8243, 771, 2447, -5197, 2556, 8044, 3314, 3617, 6 6065, -2817, 3131, 6318, 2186, -113, 629, -2582, -37, 7 -1520, 164, 2055, -5936, 5912, 1717, 5988, 4781, 5757, 8 892, -4394, 8034, 2213, -1080, -2080, 5364, 106, 2657, 9 566, 3940, -5116, 4583, 1806, 6555, 2621, -7197, 528, 10 1626, 18, 1049, 6243, 3198, 4397, -1325, 9087, 936, 11 -6291, 662, -178, 135, -3473, -2385, -165, 1713, -7949, 12 -4234, 1138, 2212, 104, 6968, -3632, 3801, 1137, -1296, 13 -1215, 4272, 6223, -5922, -7723, 7044, -2938, -8180, 14 1356, 1159, -4022, -3713, 1158, -8715, -4081, -2541, 15 -2555, -2284, 461, 940, 6604, -3631, 3802, -2037, 16 -4354, -1213, 767 }, 17 { 2740, -4182, -5632, -2966, -1953, 567, -8570, 1046, 2211, 18 1572, -2503, -1899, 3183, -6187, 3330, 3492, -464,-602, 19 -2104, 316, 8136, 470, 50, 466, -1424, 5809, 2131, 20 6418, -3018, 6002, -8379, 1433, 1144, 2124, 1624, 21 -5518, 5872, 870, -5175, -3961, -427, -6284, 2850, 481, 22 6175, 141, -766, -1897, -748, -4248, 366, 4823, 3003, 23 1778, 3256, 2182, 2253, 5076, 5540, -2650, 2451, -1875, 24 5482, -6881, -329, -969, -8032, -2093, 612, 1524, 25 -5492, 5758, -7401, -5039, 3241, 6338, 3581, 4321, 26 -1072, 4942, 2131, 210, -7045, -7514, 7450, -1142, 27 -2666, -4485, -639, 2121, -5298, -3805, -1686, -2520, 28 -1680, 2321, -4617, -1961, 2076, 7309 } }; 29 int sum=0; 30 Scanner bb = new Scanner(System.in); 31 System.out.println("startline:"); 32 int startI = bb.nextInt(); 33 System.out.println("endline:"); 34 int endI = bb.nextInt(); 35 System.out.println("startline:"); 36 int startJ = bb.nextInt(); 37 System.out.println("endline:"); 38 int endJ = bb.nextInt(); 39 System.out.println("startline" + startI + "endline" + startJ + "startline" + endI + "endline"+ endJ + "和为:"); 40 for (int i = startI - 1; i < endI; i++) 41 {for (int j = startJ - 1; j < endJ ; j++) 42 { 43 sum += b[i][j]; 44 } 45 } 46 System.out.println(b[startI - 1][startJ - 1] + "+...+" 47 + b[endI - 1][endJ - 1] + "=" + sum); 48 } 49 }
运行结果如下:
4.作业感想:本次作业遇到要将CSV格式的数据导入到MyEclipse里,然而百度并不能解决问题,后来还是借用了张振渊同学从文件里提取出来的数据(我是连接),在这里要感谢@zzy_01。代码方面则是向谢江峰同学请教了思路,写出了一个实现基本要求的代码。