软件工程第二次作业
2015-09-24 15:06 德玛西亚国王 阅读(169) 评论(4) 编辑 收藏 举报1 package work; 2 3 import java.util.Scanner; 4 5 import java.util.Arrays;//导入支持此次程序的JAVA包 6 7 public class work22 { 8 public static void main(String[] args) { 9 int [] array1 ={-2574,-4010,8243,771,2447,-5197,2556,8044,3314, 10 3617,6065,-2817,3131,6318,2186,-113,629,-2582,-37, 11 -1520,164,2055,-5936,5912,1717,5988,4781,5757,892, 12 -4394,8034,2213,-1080,-2080,5364,106,2657,566,3940, 13 -5116,4583,1806,6555,2621,-7197,528,1626,18,1049, 14 6243,3198,4397,-1325,9087,936,-6291,662,-178,135, 15 -3473,-2385,-165,1713,-7949,-4234,1138,2212,104, 16 6968,-3632,3801,1137,-1296,-1215,4272,6223,-5922, 17 -7723,7044,-2938,-8180,1356,1159,-4022,-3713,1158, 18 -8715,-4081,-2541,-2555,-2284,461,940,6604,-3631,3802,-2037,-4354,-1213,767};//定义数组并赋值,属于静态数组 19 int sum=0; 20 Scanner scanner = new Scanner(System.in) ;//要从键盘输入数据,采用Scanner类的nextInt()方法 21 System.out.println("请输入一个起始数:"); 22 int n=scanner.nextInt(); 23 System.out.println("请输入一个终止数:"); 24 int t=scanner.nextInt(); 25 System.out.println("起始数:"+n+"终止数:"+t); 26 for(int i =n;i<=t;i++){//用for语句来在起始数和终止数中做一个循环加法。 27 sum+=array1 [i] ; 28 System.out.println("sum="+sum); 29 30 } 31 } 32 }
运行结果演示:
此处运行结果让我感到非常奇怪,为什么会有多行结果输出。重新翻看书本,发现for语句的循环出了点问题。经改正后运行结果如下:
问题在与for语句循环中:
for(int i =n;i<=t;i++){ sum+=array1 [i] ;} System.out.println("sum="+sum); for(int i =n;i<=t;i++){ sum+=array1 [i] ; System.out.println("sum="+sum); }
for语句循环中一个{}的错误。
二维数组:
package work; import java.util.Scanner; public class work2a {public static void main(String[]args){ int[][] array2={{-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 scanner = new Scanner(System.in) ;//与上一个程序相同,都需要Scanner类。 System.out.println("请输入一个起始数x1:");//与上一个程序不同,因为是二维数组,所以先输入两个x轴或者y轴的数,再相加。 int x1=scanner.nextInt(); System.out.println("请输入一个终止数x2:"); int x2=scanner.nextInt(); System.out.println("起始数:"+x1+"终止数:"+x2); System.out.println("请输入一个起始数y1:"); int y1=scanner.nextInt(); System.out.println("请输入一个起始数y2:"); int y2=scanner.nextInt(); System.out.println("起始数:"+y1+"终止数:"+y2); for(int i=x1;i<=x2;i++)for(int j=y1;j<=y2;j++){ sum+=array2[i][j];} System.out.println("sum="+sum); } }
下面是演示结果:
个人经验总结:
我觉得是在代码的优化上,我们还需要更加努力,比如说java如何读取excel中的数据或者xml文件中的数据,在网上找到的资料也是看的云里雾里。毕竟我的基础还是太薄弱了。在写这些代码的过程中,尝试了很多不同的表现方式。比如说最后的计算部位,尝试过用for-if语句。发现自己对if语句的认识非常之少,所以放弃了多种组合语句计算的方式,还需要多多的回顾以前javaEE的教程。只有在基础上的运用达到娴熟的程度,才能接触到更高层次的语法。在写第一个程序的时候,我在开头导入了java.util.Arrays这个包,但是当我把整个程序全部完成后,这个包也没被使用过。可能是因为我只是将数组中的数据手动COPY了上去,没有用到数组引用或者遍历的效果。所以在第二个程序中并没有导入这个包,最后程序也是顺利的运行了。其中还有很多细小的错误,都被逐个的纠正了。所以,我觉得写代码是一件细活,不能急躁的想要出结果,而是一步步的实现我所需要的需求,最终组合起来就是我想要的结果。