1. 设计一个类(不妨叫做A ),实现对一维数组的指定范围内的元素相加运算。
package vvv;
import java.util.Scanner;
public class A {
public static void main(String[]args)
{
int[] arr1={767,-4619,2209,-7862,-3195,5676,4553,-784,-2010,-3046,1566,-1562,1021,2943,-7241,3847,3222,3001,-1731,-4889,2544,-7893,-1142,7523,7559,867,-4088,2001,-2532,-4488,-8557,5088,-7906,-2104,-1911,-1048,1886,5500,-890,1386,-2080,1431,-1607,5808,-8107,4418,450,1261,-3869,-1184,418,2413,-6418,4607,4059,-444,7821,-2605,-2241,-1390,3047,-1588,8529,1116,4085,4512,4008,4242,6382,-1824,2455,-9217,6014,-2490,-7241,-845,9680,-3756,-5569,3402,4704,-6267,-2393,3191,-965,-5042,-6951,-6184,-8752,525,2144,6195,541,-3336,6904,-3130,-3261,-4770,-34,-1991,};
int sum=0;
Scanner se = new Scanner(System.in) ;
System.out.println("请输入开始数x_:");
int start = se.nextInt() ;
System.out.println("请输入结束数x_:");
int end =se.nextInt();
System.out.println("x开始为:" +start);
System.out.println("x结束为:" +end);
for(int i=start;i<=end;i++)
sum+=arr1[i];
System.out.println(sum);
}
}
2.设计一个类(不妨叫做B ),实现对二维数组的指定范围内的元素相加运算。
package _007;
import java.util.Scanner;
public class B {
public static void main(String[]args){
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}};
int sum=0;
Scanner se = new Scanner(System.in);
System.out.println("输入start-y:");
int start1 = se.nextInt();
System.out.println("输入end-y:");
int end1 =se.nextInt();
System.out.println("start-y:" +start1);
System.out.println("end-y:" +end1);
System.out.println("输入start-x:");
int start2 = se.nextInt();
System.out.println("输入end-x:");
int end2 =se.nextInt();
System.out.println("start-x:" +start2);
System.out.println("end-x:" +end2);
for(int i=start1;i<=end1;i++)
{
for(int j=start2;j<=end2;j++)//
{
sum+=a[i][j];
}
}
System.out.println(sum);
}
}
运行结果与下图所示:
3. 设计一个类(不妨叫做C ),实现对三维数组的指定范围内的元素相加运算。
package _007;
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}}
};//这边的三位数组定义是宝玉老师给的建议,看起来比较抽象,因为是y=0的三维数组。
Scanner se = new Scanner(System.in) ;
System.out.println("请输入start-x:");
int start1 = se.nextInt() ;
System.out.println("请输入end-x:");
int end1 =se.nextInt();
System.out.println("start-x为:" +start1);
System.out.println("end-x为:" +end1);
System.out.println("请输入start-y:");
int start2 = se.nextInt() ;
System.out.println("请输入end-y:");
int end2 =se.nextInt();
System.out.println("start-y为:" +start2);
System.out.println("end-y为:" +end2);
System.out.println("请输入start-z:");
int start3 = se.nextInt() ;
System.out.println("请输入end-z:");
int end3 =se.nextInt();
System.out.println("start-z为:" +start3);
System.out.println("end-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(sum);
}
}
代码编写经历和小结:
一开始在看老师给的要求时,觉得好难。根本不知道该怎么去完成,一直抱着畏惧心理。感觉好难,所以才拖到现在才完成。
这几天通过研究群里面老师推荐的几份完成的不错的作业,好好咨询了下已经完成作业的同学,寻求他们的帮助,终于完后了这份作业。
才开始根本不知道如何下手,后面请教了林杰同学、后面在他的帮助下,借鉴了他的代码同时听他给我讲解了代码的原理。在完成这份作业时遇到好多不懂得问题,比如如何用人工的方法把Excel中的数据变成数组,等等好多问题与难题。
在完成作业的同时也得到了解决,对代码有了更深的看法,学会了好多以前自己不懂得操作。
在这里谢谢林杰同学的帮忙。