第二次作业(1.个人项目实践)
2015-09-24 12:40 德玛西亚皇子 阅读(198) 评论(1) 编辑 收藏 举报源代码:
1 package aaa;
2 import java.util.Scanner;//用于java输入语句的包。
3
4 public class A {
5
6 public static void main(String[]args)
7 {
8 int[] arr1={-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};//因为实现不用Java写程序导入数据,所以这里直接将数据copy过来了。
9 int sum=0;
10 Scanner se = new Scanner(System.in) ;//因为要从键盘输入你想要取的开始和结束的数,所以这里用到Scanner类的nextInt()方法。
11 System.out.println("请输入start:");
12 int start = se.nextInt() ;
13 System.out.println("请输入end:");
14 int end =se.nextInt();
15 System.out.println("start为:" +start);
16 System.out.println("end为:" +end);
17 for(int i=start;i<=end;i++)/*这边用start/end做下标(start/end是你在上边键盘输入的),再用for语
18 句循环和sum+=arr1[]进行累加可以表示出你要取的范围,例如start:2,end=9,
19 就可以输出上述一维数组中下标为2到9的数的和。*/
20 sum+=arr1[i];
21 System.out.println(sum);
22 }
23
24 }
arr1测试:验证:
1 package aaa;
2
3 import java.util.Scanner;
4
5 public class B {
6
7 public static void main(String[]args){
8 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}};
9 int sum=0;
10 Scanner se = new Scanner(System.in);
11 System.out.println("输入start-y:");
12 int start1 = se.nextInt();
13 System.out.println("输入end-y:");
14 int end1 =se.nextInt();
15 System.out.println("start-y:" +start1);
16 System.out.println("end-y:" +end1);
17
18 System.out.println("输入start-x:");
19 int start2 = se.nextInt();
20 System.out.println("输入end-x:");
21 int end2 =se.nextInt();
22 System.out.println("start-x:" +start2);
23 System.out.println("end-x:" +end2);
24 for(int i=start1;i<=end1;i++)
25 {
26 for(int j=start2;j<=end2;j++)//类比一维数组可以得出。
27 {
28 sum+=a[i][j];
29 }
30 }
31 System.out.println(sum);
32 }
33
34 }
arr2测试:验证:
1 package aaa;
2
3 import java.util.Scanner;
4
5 public class C {
6
7
8 public static void main(String[]args)
9 {
10 int sum=0;
11 int[][][] a = new int[][][]
12 {
13 {{-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}},
14 {{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}},
15 {{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}}
16 };//这边的三位数组定义是宝玉老师给的建议,看起来比较抽象,因为是y=0的三维数组。
17 Scanner se = new Scanner(System.in) ;
18 System.out.println("请输入start-x:");
19 int start1 = se.nextInt() ;
20 System.out.println("请输入end-x:");
21 int end1 =se.nextInt();
22 System.out.println("start-x为:" +start1);
23 System.out.println("end-x为:" +end1);
24
25 System.out.println("请输入start-y:");
26 int start2 = se.nextInt() ;
27 System.out.println("请输入end-y:");
28 int end2 =se.nextInt();
29 System.out.println("start-y为:" +start2);
30 System.out.println("end-y为:" +end2);
31
32 System.out.println("请输入start-z:");
33 int start3 = se.nextInt() ;
34 System.out.println("请输入end-z:");
35 int end3 =se.nextInt();
36 System.out.println("start-z为:" +start3);
37 System.out.println("end-z为:" +end3);
38 for(int i=start1;i<=end1;i++)
39 {
40 for(int j=start2;j<=end2;j++)
41 {
42 for(int q=start3;q<=end3;q++)//类比二维数组可以得出。
43 {
44 sum+=a[i][j][q];
45
46 }
47 }
48
49 }
50 System.out.println(sum);
51 }
52 }
arr3:验证: