码迷,mamicode.com
首页 > 其他好文 > 详细

第三次作业

时间:2015-11-18 16:32:09      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:

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的时候遇到过,然而我并没有记住并实践。

  

     

第三次作业

标签:

原文地址:http://www.cnblogs.com/sumnus5120/p/4974664.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!