第2次作业(个人项目实践)
1. 设计一个类(不妨叫做A ),实现对一维数组的指定范围内的元素相加运算。
1 package yang; 2 3 import java.util.Scanner;//用scanner类从键盘输入数据 4 5 public class yy { 6 public static void main(String[]args){ 7 8 int[] aa={-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}; 9 10 int sum=0;//用于统计和 11 Scanner a = new Scanner(System.in); //定义类的对象a,接收来自键盘的数据 12 System.out.println("从何开始?:"); 13 int start = a.nextInt() ; 14 System.out.println("到哪结束?:"); 15 int end =a.nextInt(); 16 System.out.println("指定范围是:"+start+"---"+end); 17 for(int i=start-1;i<=end-1;i++) 18 sum+=aa[i];//此处循环 19 System.out.println("运算和:"); 20 System.out.println(sum); 21 } 22 }
运算结果为:
2. 设计一个类(不妨叫做B ),实现对二维数组的指定范围内的元素相加运算。
1 package yang; 2 import java.util.Scanner; 3 public class yy2 { 4 public static void main(String[] args) { 5 int[][] b={ 6 { -2574,-4010,8243,771,2447,-5197,2556,8044,3314,3617,6065,-2817,3131,6318,2186,-113,629,-2582,-37,-1520, 7 164,2055,-5936,5912,1717,5988,4781,5757,892,-4394,8034,2213,-1080,-2080,5364,106,2657,566,3940,-5116, 8 4583,1806,6555,2621,-7197,528,1626,18,1049,6243,3198,4397,-1325,9087,936,-6291,662,-178,135,-3473,-2385, 9 -165,1713,-7949,-4234,1138,2212,104,6968,-3632,3801,1137,-1296,-1215,4272,6223,-5922,-7723,7044,-2938,-8180, 10 1356,1159,-4022,-3713,1158,-8715,-4081,-2541,-2555,-2284,461,940,6604,-3631,3802,-2037,-4354,-1213,767}, 11 { 2740,-4182,-5632,-2966,-1953,567,-8570,1046,2211,1572,-2503,-1899,3183,-6187,3330,3492,-464,-2104,316,8136, 12 470,50,466,-1424,5809,2131,6418,-3018,6002,-8379,1433,1144,2124,1624,-602,-5518,5872,870,-5175,-3961,-427, 13 -6284,2850,481,6175,141,-766,-1897,-748,-4248,366,4823,3003,1778,3256,2182,2253,5076,5540,-2650,2451,-1875, 14 5482,-6881,-329,-969,-8032,-2093,612,1524,-5492,5758,-7401,-5039,3241,6338,3581,4321,-1072,4942,2131,210,-7045, 15 -7514,7450,-1142,-2666,-4485,-639,2121,-5298,-3805,-1686,-2520,-1680,2321,-4617,-1961,2076,7309} 16 }; 17 18 Scanner b1 = new Scanner(System.in); 19 System.out.println("横轴开始:");//把二维数组看做一个数轴 20 int startA = b1.nextInt(); 21 System.out.println("横轴结束:"); 22 int endA =b1.nextInt(); 23 System.out.println("竖轴开始:"); 24 int startD = b1.nextInt(); 25 System.out.println("数轴结束:"); 26 int endD =b1.nextInt(); 27 System.out.println("指定范围为:"); 28 System.out.println("横轴"+startA+"---"+endA); 29 System.out.println("竖轴"+startD+"---"+endD); 30 31 int j=startD-1; 32 int sum=0; 33 for(int i=startA-1;i<endA-1; i++) //利用for while循环嵌套语句求和 34 { 35 while(j<endD-1) 36 { 37 j++; 38 sum=sum+b[i][j]; 39 } 40 } 41 42 System.out.println("总和:"+sum); 43 44 } 45 }
运算结果为:
这次作业做的可以算是历经沧桑_(:зゝ∠)_,数组 虽然以前学过,但也就是一个模糊的概念,也没有想要把它明了话,但是如今为了把作业写好,来来回回把书翻了好几遍,还去问了已经做好的同学,如果以前学习我也能有这态度,说不定现在编写代码就没有这么痛苦了。不过也因为这次作业,让我学到了许多知识,像利用scanner类接收从键盘输入的信息。scanner类不仅可以从键盘读取数据,还可以从其他数据源读取。在这次的代码编写中,我发现我很容易就会漏掉大括号,分号等等一些细小的问题,往往因为这些问题导致代码出现大部分错误,还常常找不出错误原因_(:зゝ∠)_,一行行代码找下来,发现因为漏掉小符号,就想说自己真的粗心的可以。
我觉得我的代码还是存在很多的问题,一维二维勉强写出来了,但是三维数组的编写感觉有点力不从心了,错误多,而且经常改掉这个错误就又会出现新的错误,有几次修改后没有报错了,一运行编译器就报错了,因为没有运行出结果,我就没把三维数组的代码贴上来,我会在之后再研究研究,把代码完善并且运行出来后,再发表随笔将他发布出来。然后,学习真的是要不停复习复习复习,因为一段时间不看,很多东西就会忘掉_(:зゝ∠)_。我会在接下来的时间好好学习,在学习新内容的同时复习以前学习过的知识!完善自己!(๑•̀ㅂ•́)و✧