1. 设计一个类(不妨叫做A ),实现对一维数组的指定范围内的元素相加运算。 [课堂讲解过] 数组长度:100,范围:实验数据A列:1~100
程序代码:
package qqq;
import java.util.Scanner;
public class A {
public static void main(String[]args)
{
int []arr=
{-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};
int result=0;
Scanner se = new Scanner(System.in) ;
System.out.println("初位置:");
int first = se.nextInt() ;
System.out.println("末位置:");
int last =se.nextInt();
for(int i=first;i<=last;i++)
result+=arr[i];
System.out.println("相加结果为:");
System.out.println(result);
}
}
运行结果:
2. 设计一个类(不妨叫做B ),实现对二维数组的指定范围内的元素相加运算。 数组长度:100,范围:实验数据A、B列:1~100
程序代码:
package qqq;
import java.util.Scanner;
public class B {
public static void main(String[]args){
int[][] arr={{-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 result=0;
Scanner se = new Scanner(System.in);
System.out.println("初始行:");
int firstH = se.nextInt();
System.out.println("终止行:");
int lastH =se.nextInt();
System.out.println("初始列:");
int firstL = se.nextInt();
System.out.println("终止列:");
int lastL =se.nextInt();
for(int b=firstH;b<=lastH;b++)
{
for(int c=firstL;c<=lastL;c++)
{
result+=arr[b][c];
}
}
System.out.println("相加结果为:");
System.out.println(result);
}
}
运行结果:
3. 设计一个类(不妨叫做C ),实现对三维数组的指定范围内的元素相加运算。 数组长度:100,范围:实验数据A、B、C列:1~100
程序代码:
package qqq;
import java.util.Scanner;
public class C {
public static void main(String[]args)
{
int result=0;
int[][][] arr = 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 se = new Scanner(System.in) ;
System.out.println("初始x:");
int firstX = se.nextInt() ;
System.out.println("终止x:");
int lastX =se.nextInt();
System.out.println("初始y:");
int firstY = se.nextInt() ;
System.out.println("终止y:");
int lastY =se.nextInt();
System.out.println("初始z:");
int firstZ = se.nextInt() ;
System.out.println("终止z:");
int lastZ =se.nextInt();
for(int b=firstX;b<=lastX;b++)
{
for(int c=firstY;c<=lastY;c++)
{
for(int d=firstZ;d<=lastZ;d++)
{
result+=arr[b][c][d];
}
}
}
System.out.println("相加结果为:");
System.out.println(result);
}
}
运行结果:
实践心得:
这次作业耗时有点久,起初是不太理解作业的意思和对数组的相关知识掌握的不是特别好,所以导致有点无从下手,一瞬间茫然不知所措的烦躁心情油然而生,而后经过老师上课时的讲解和询问同学有了进一步的理解,把Java书上的有关数组的内容又看了一遍(只怪当初上课时没能够足够的认真听讲),开始了这次作业的完成。然而,一切并不是想象中的那么顺利,完成一维数组时还好,而完成二维数组和三维数组时就开始出现了很多的问题,起初把二维数组、三维数组的设计和一维数组弄混淆了,结果导致了二维数组、三维数组和一维数组设计的程序差不多,还以为已经完成了作业,后来看到了同学提交的作业时才发现自己想的太简单了,又开始了对二维数组和三维数组的重新设计,而在代码运行过程中又遇到了一个抓狂的问题:在输入起始数和终止数后运行累计和结果的时候一直报错,运行结果出不来。经过一番折腾后才发现自己忽视了一个很明显的问题,在二维数组和三维数组输入起止数时是要注意范围的,而不是随意输入几个数字就行的,这才一步步的完成了这次作业。在这次作业完成的过程中,还是发现了自己的很多问题,首先是对学过的知识掌握不牢而导致作业无法下手;其次是自己遇到问题时容易烦躁,导致了一些很浅显的问题在自己思路不是很清晰的时候发现不了,从而导致了出现很多小错误,不过我相信这些问题会在以后的实践中会慢慢改善的,实践的目的也就是让我们逐渐得到提升。