1、设计一个类(不妨叫做A ),实现对一维数组(a[100])的指定范围内的元素相加运算。
程序代码如下:
package df; import java.util.Scanner;//用于java输入语句的包。 public class a { public static void main(String[]args) { int[] w={-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};//直接将数据copy过来 int sum=0; Scanner hh = new Scanner(System.in) ;//实例化。 System.out.println("从第几个数开始:"); int start = hh.nextInt() ; //这里用到Scanner类的nextInt()方法 System.out.println("到第几个数结束:"); int end =hh.nextInt(); System.out.println("开始为:" +start); System.out.println("结束为:" +end); for(int i=start;i<=end;i++) sum+=w[i]; System.out.println(sum); } }
代码运行如下:
2、 设计一个类(不妨叫做B ),实现对二维数组的指定范围内的元素相加运算。
程序代码如下:
package df; import java.util.Scanner; public class b { public static void main(String[]args){ int[][]b={ {-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 hh = new Scanner(System.in); System.out.println("从第几行开始:");//一共两行 int begin1 = hh.nextInt(); System.out.println("到第几行结束:"); int over1 =hh.nextInt(); System.out.println("从第几列开始:");//<=100 int begin2 = hh.nextInt(); System.out.println("到第几列结束:"); int over2 =hh.nextInt(); System.out.println("第"+begin1+"行第"+begin2+"列到第"+over1+"行第"+over2+"列的和为:"); for(int i=begin1-1;i<=over1-1;i++) { for(int j=begin2-1;j<=over2-1;j++) { sum+=b[i][j]; } } System.out.println(sum); } }
代码运行结果:
3、 设计一个类(不妨叫做C ),实现对三维数组的指定范围内的元素相加运算
代码如下:
package df; import java.util.Scanner; public class c { public static void main(String[]args) { int sum=0; int[][][] a = new int[][][] { {{-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}}, {{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}} }; Scanner hh = new Scanner(System.in) ; System.out.println("开始x:"); int start1 = hh.nextInt() ; System.out.println("结束x:"); int end1 =hh.nextInt(); System.out.println("开始x为:" +start1); System.out.println("结束x为:" +end1); System.out.println("开始y:"); int start2 = hh.nextInt() ; System.out.println("结束y:"); int end2 =hh.nextInt(); System.out.println("开始y:" +start2); System.out.println("结束y为:" +end2); System.out.println("开始z:"); int start3 = hh.nextInt() ; System.out.println("结束z:"); int end3 =hh.nextInt(); System.out.println("开始z为:" +start3); System.out.println("结束z为:" +end3); for(int i=start1;i<=end1;i++) { for(int j=start2;j<=end2;j++) { for(int q=start3;q<=end3;q++) { sum+=a[i][j][q]; } } } System.out.println("相加结果为:") System.out.println(sum); } }
代码运行如下:
4、总结
在老师布置这次作业的时候,便联想到大二时曾经学过Java中用for语句1+2+3+.....+100的问题,但是结合题目看了看好像自己想的太简单了,于是便又去查询了一下课本,发现似乎可以运用Scanner类的nextInt()方法再加上for循环语句加以解决这个问题。但是对于以前知识薄弱的我来说写这个程序还是有点儿困难,于是便去请教室友,在室友的帮助下,终于渐渐有了代码雏形,便开始自己动手去写,可是在调试的过程中,一直出现错误,但是找了半天却依旧找不出来,幸亏老师有推荐两位写的比较好的同学的作业,于是便却拜读了一下,才发现自己对于数组取值的范围太过随便。最后终于得出了结果。这次作业,让我深刻明白自己在这方年面的欠缺,当然也体验到合作完成任务的快乐。希望以后能够渐渐完善自己,在以后的作业完成的过程中更进一步。
时间: 2024-11-01 16:29:16