第二次作业(个人项目实践)
1. 設計一個類(不妨叫做A ),實現對一維數組(a[100])的指定范圍內的元素相加運算。
1 package sijue; 2 import java.util.Scanner;//用于從鍵盤導入數據 3 4 public class A { 5 public static void main(String[]args){ 6 int[] list={-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}; 7 int sum=0;//存放和 8 Scanner a= new Scanner(System.in) ;//Scanner類的對象a用來接收來自鍵盤的數據 9 System.out.println("請輸入起點:"); 10 int x=a.nextInt(); 11 System.out.println("請輸入終點:"); 12 int y=a.nextInt(); 13 System.out.println("從"+x+"到"+y+"的和為"); 14 for(int i=x;i<=y;i++) 15 sum+=list[i]; //循環 16 System.out.println(sum); 17 } 18 }運行結果:
2. 設計一個類(不妨叫做B ),實現對二維數組(a[N][M])的指定范圍內的元素相加運算。
1 package sijue; 2 import java.util.Scanner; 3 public class B { 4 public static void main(String[]args){ 5 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}}; 6 int sum=0; 7 Scanner b = new Scanner(System.in); 8 System.out.println("起始行:"); 9 int x1 = b.nextInt(); 10 System.out.println("終點行");//x取值為1-2 11 int y1 =b.nextInt(); 12 System.out.println("起始列:"); 13 int x2 = b.nextInt(); 14 System.out.println("終點列:");//y取值為1-100 15 int y2 =b.nextInt(); 16 System.out.println("從("+x1+","+x2+")到"+"("+y1+","+y2+")的和為"); 17 for(int i=x1-1;i<=y1-1;i++) 18 { 19 for(int j=x2-1;j<=y2-1;j++) 20 { 21 sum+=a[i][j]; 22 } 23 } 24 System.out.println(sum); 25 } 26 }運行結果:
3.?設計一個類(不妨叫做C?),實現對三維數組的指定范圍內的元素相加運算。
1 package sijue; 2 import java.util.Scanner; 3 public class C { 4 public static void main(String[]args){ 5 int sum=0; 6 int[][][] a= new int[][][] 7 { 8 {{-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 {{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}}, 10 {{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}} 11 }; 12 Scanner c= new Scanner(System.in) ; 13 System.out.println("x軸起點:"); 14 int x1 = c.nextInt() ; 15 System.out.println("x軸終點:");//x取值為1-3 16 int x2 =c.nextInt(); 17 System.out.println("y軸起點:"); 18 int y1 = c.nextInt() ; 19 System.out.println("y軸終點:");//y取值為1 20 int y2 =c.nextInt(); 21 System.out.println("z軸起點:"); 22 int z1 = c.nextInt() ; 23 System.out.println("z軸終點:");//z取值為1-100 24 int z2 =c.nextInt(); 25 System.out.println("從("+x1+","+y1+","+z1+")到"+"("+x2+","+y2+","+z2+")的和為:"); 26 for(int i=x1-1;i<=x2-1;i++) 27 { 28 for(int j=y1-1;j<=y2-1;j++) 29 { 30 for(int m=z1-1;m<=z2-1;m++) 31 { 32 sum+=a[i][j][m]; 33 } 34 } 35 } 36 System.out.println(sum); 37 } 38 }運行結果:
小結:
? ? 這次的作業完成的極其不順利,在一開始老師布置作業的時候,我就有點懵,完全不懂老師讓做什么,再加上本身變成并不拿手,這個作業也擱淺了相當久,但是在看到老師和助教在QQ群里的講解后就恍然大悟了,也借鑒了同學的思想,比如說Scanner類的nextInt方法可從鍵盤導入數據,這個用法在學習Java的時候遇到過,然而我并沒有記住并實踐,所以這次也就只好“拿來主義”了。
? ?在運行二維數組的過程中,我遇到一個問題,如下圖:
? ? ? ?這個問題讓我百思不得其解,感覺莫名其妙,因為代碼沒有報錯卻沒有運行結果,于是我就看了其他同學的隨筆,發現問題出在了取值范圍上,數據其實只有兩行,每行有一百個,而我輸入終點坐標的是(30,80),很明顯是沒有第三十行的,所是無法得出結果。
? ? ? 做完這次實踐,我更加明白了,同學之間合作的重要,還有遇到問題要及時詢問老師,憋著不說是不可能有結果的,最后,一定要認真仔細,有時候一個符號都會影響結果。
轉載于:https://www.cnblogs.com/sijue1008/p/4836000.html
總結
以上是生活随笔為你收集整理的第二次作业(个人项目实践)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Apache 配置支持HTTPS的SSL
- 下一篇: DOM操作表格