第二次作业:个人项目实践
1. 设计一个类(不妨叫做A ),实现对一维数组(a[100])的指定范围内的元素相加运算。 数组长度:100,范围:实验数据A列:1~100,CSV 格式则填充 前100个数据
1 import java.util.Scanner; 2 3 public class A { 4 public static void main(String[] args) { 5 int[] aa = { -2574, -4010, 8243, 771, 2447, -5197, 2556, 8044, 3314, 6 3617, 6065, -2817, 3131, 6318, 2186, -113, 629, -2582, -37, 7 -1520, 164, 2055, -5936, 5912, 1717, 5988, 4781, 5757, 892, 8 -4394, 8034, 2213, -1080, -2080, 5364, 106, 2657, 566, 3940, 9 -5116, 4583, 1806, 6555, 2621, -7197, 528, 1626, 18, 1049, 10 6243, 3198, 4397, -1325, 9087, 936, -6291, 662, -178, 135, 11 -3473, -2385, -165, 1713, -7949, -4234, 1138, 2212, 104, 6968, 12 -3632, 3801, 1137, -1296, -1215, 4272, 6223, -5922, -7723, 13 7044, -2938, -8180, 1356, 1159, -4022, -3713, 1158, -8715, 14 -4081, -2541, -2555, -2284, 461, 940, 6604, -3631, 3802, -2037, 15 -4354, -1213, 767 };// 不会使用CSV格式,只能使用这种方法 16 int sum = 0; 17 Scanner a = new Scanner(System.in); 18 System.out.println("从第几个开始(<=100):"); 19 int start = a.nextInt(); 20 System.out.println("到第几个结束(<=100):"); 21 int end = a.nextInt(); 22 System.out.println("第" + start + "个元素到第" + end + "个元素的和为:"); 23 24 for (int i = start - 1; i < end; i++) 25 // 第一个是aa[0] 26 sum += aa[i]; 27 System.out.println(aa[start - 1] + "+...+" + aa[end - 1] + "=" + sum); 28 29 } 30 }
输出结果:(正确的情况)
(超出的情况)
2. 设计一个类(不妨叫做B ),实现对二维数组(a[N][M])的指定范围内的元素相加运算。 数组长度:(N*M 约等于200)A、B列:1~100;
1 import java.util.Scanner; 2 3 public class B { 4 public static void main(String[] args) { 5 int[][] b = { 6 { -2574, -4010, 8243, 771, 2447, -5197, 2556, 8044, 3314, 3617, 7 6065, -2817, 3131, 6318, 2186, -113, 629, -2582, -37, 8 -1520, 164, 2055, -5936, 5912, 1717, 5988, 4781, 5757, 9 892, -4394, 8034, 2213, -1080, -2080, 5364, 106, 2657, 10 566, 3940, -5116, 4583, 1806, 6555, 2621, -7197, 528, 11 1626, 18, 1049, 6243, 3198, 4397, -1325, 9087, 936, 12 -6291, 662, -178, 135, -3473, -2385, -165, 1713, -7949, 13 -4234, 1138, 2212, 104, 6968, -3632, 3801, 1137, -1296, 14 -1215, 4272, 6223, -5922, -7723, 7044, -2938, -8180, 15 1356, 1159, -4022, -3713, 1158, -8715, -4081, -2541, 16 -2555, -2284, 461, 940, 6604, -3631, 3802, -2037, 17 -4354, -1213, 767 }, 18 { 2740, -4182, -5632, -2966, -1953, 567, -8570, 1046, 2211, 19 1572, -2503, -1899, 3183, -6187, 3330, 3492, -464, 20 -2104, 316, 8136, 470, 50, 466, -1424, 5809, 2131, 21 6418, -3018, 6002, -8379, 1433, 1144, 2124, 1624, -602, 22 -5518, 5872, 870, -5175, -3961, -427, -6284, 2850, 481, 23 6175, 141, -766, -1897, -748, -4248, 366, 4823, 3003, 24 1778, 3256, 2182, 2253, 5076, 5540, -2650, 2451, -1875, 25 5482, -6881, -329, -969, -8032, -2093, 612, 1524, 26 -5492, 5758, -7401, -5039, 3241, 6338, 3581, 4321, 27 -1072, 4942, 2131, 210, -7045, -7514, 7450, -1142, 28 -2666, -4485, -639, 2121, -5298, -3805, -1686, -2520, 29 -1680, 2321, -4617, -1961, 2076, 7309 } }; 30 int sum=0; 31 Scanner bb = new Scanner(System.in); 32 System.out.println("从第几列开始:"); 33 int startI = bb.nextInt(); 34 System.out.println("到第几列结束:"); 35 int endI = bb.nextInt(); 36 System.out.println("从第几行开始:"); 37 int startJ = bb.nextInt(); 38 System.out.println("到第几行结束:"); 39 int endJ = bb.nextInt(); 40 System.out.println("第" + startI + "列第" + startJ + "行到第" + endI + "列第" 41 + endJ + "行的和为:"); 42 43 for (int i = startI - 1; i < endI; i++) 44 { 45 for (int j = startJ - 1; j < endJ ; j++) 46 { 47 sum += b[i][j]; 48 } 49 } 50 System.out.println(b[startI - 1][startJ - 1] + "+...+" 51 + b[endI - 1][endJ - 1] + "=" + sum); 52 } 53 }
输出结果:
(超出的图就不附上了)
总结:
做一维和二维数组的作业就花了很多时间,三维数组要建立空间结构,实在想不明白,问过张振渊同学,但依然一知半解,就放弃了三维数组的作业。
对于用CSV格式导入Excel数据,根本不会,,看了老师给的方法,完全看不懂,百度了下也还是一头雾水,宁愿用简单点的方式,所以还是采用了输数字的方法,还好是100个,不太费力。由于是手动输入的100个数字,也就规定了一维数组中i和二维数组中j的范围是<=100,于是也没在系统中定义一下i和j的范围,反正也是显而易见的。
不过不得不说,这两个程序局限性太大,不适合广泛使用,数字都是硬输进去的,希望经过这一学期的学习,能学到点东西做出一个完美谈不上,但实用的程序吧。