第二次作业

 1. 设计一个类(不妨叫做A ),实现对一维数组(a[100])的指定范围内的元素相加运算。  

package abc;
import java.util.Scanner;
public class A {
      public static void main(String[] args) {
                int[] aa={-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 sum=0;
               Scanner a = new Scanner(System.in);
               System.out.println("从这个数开始:");
                int start = a.nextInt();
               System.out.println("到这个数结束:");
                int end =a.nextInt();
               System.out.println("总和为:");

               for(int i=start-1;i<=end-1;i++)
              sum+=aa[i];
              System.out.println(aa[start-1]+"+...+"+aa[end-1]+"="+sum);
}

}

 


2. 设计一个类(不妨叫做B ),实现对二维数组(a[N][M])的指定范围内的元素相加运算。  

package abc;
import java.util.Scanner;
public class B {
public static void main(String[] args) {
int[][] bb={{-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 b = new Scanner(System.in);
System.out.println("从第几行开始:");
int start1 = b.nextInt();
System.out.println("到第几行结束:");
int end1 =b.nextInt();

System.out.println("从第几列开始:");
int start2 = b.nextInt();
System.out.println("到第几列结束:");
int end2 =b.nextInt();
System.out.println("总和为:");

for(int i=start1-1;i<=end1-1;i++)
for(int j=start2-1;j<=end2-1;j++)
sum+=bb[i][j];
System.out.println(bb[start1-1][start2-1]+"+...+"+bb[end1-1][end2-1]+"="+sum);
}


}



3. 设计一个类(不妨叫做C ),实现对三维数组(a[N][M][K])的指定范围内的元素相加运算。 

package abc;
import java.util.Scanner;
public class C {

public static void main(String[] args) {
int[][][] cc={{{-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}}};

int sum=0;
Scanner c = new Scanner(System.in);
System.out.println("从x1开始:");
int start1 = c.nextInt();
System.out.println("到x2数结束:");
int end1 =c.nextInt();
System.out.println("从y1数开始:");
int start2 = c.nextInt();
System.out.println("到y2数结束:");
int end2 =c.nextInt();
System.out.println("从z1数开始:");
int start3 = c.nextInt();
System.out.println("到z2数结束:");
int end3 =c.nextInt();
System.out.println("总和为:");

for(int i=start1-1;i<=end1-1;i++)
for(int j=start2-1;j<=end2-1;j++)
for(int k=start3-1;k<=end3-1;k++)
sum+=cc[i][j][k];
System.out.println(cc[start1-1][start2-1][start3-1]+"+...+"+cc[end1-1][end2-1][end3-1]+"="+sum;

}

}

 

 

小结:

      开始做这次作业,才知道要能写出完整的代码真的不容易,数组的知识也都不记得了。本次作业用到了java中Scanner类中的nextInt()方法,还有for循环语句,类的定义等知识。 
      一开始是真的什么都不懂,光看题目整个人都蒙了,经过两天的研究还有同学老师的指点,我才慢慢懂得这些代码的意义,我的代码是我看过之前同学交的作业理解过后类比写的,还有不完善的地方。刚开始的调试我的数据都是1到100随便输的,然后在调总和的时候总是出现这段话,

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 31
at abc.C.main(C.java:30) 通过百度也没能知道到底什么原因,后来问过林杰同学才知道数据有范围。

      在实践中,我明白了我的不足,我会不断学习,不断进步,在以后的作业中写出更好的代码。

      

你可能感兴趣的:(第二次作业)