个人项目实验

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

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("从第几个开始(<=100):");      

                int start = a.nextInt();       

               System.out.println("到第几个结束(<=100):");    

                int end = a.nextInt();    

               System.out.println("第" + start + "个元素到第" + end + "个元素的和为:");

 

            for (int i = start - 1; i < end; i++)         

                  // 第一个是aa[0]          

                        sum += aa[i];        

          System.out.println(aa[start - 1] + "+...+" + aa[end - 1] + "=" + sum);

    }

}

 

运行结果为:开始数1~结束数10的元素相加为17211

               

 

 

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

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 bb = new Scanner(System.in);
        System.out.println("开始列:");
         int startI = bb.nextInt();
       System.out.println("结束列:");
        int endI = bb.nextInt();
        System.out.println("开始行:");
        int startJ = bb.nextInt();
         System.out.println("结束行:");
         int endJ = bb.nextInt();
         System.out.println("开始列" + startI + "结束列" + startJ + "开始行" + endI + "结束行"
                 + endJ + "和为:");

         for (int i = startI - 1; i < endI; i++)
         {
             for (int j = startJ - 1; j < endJ ; j++)
             {
                 sum += b[i][j];
            }
         }
         System.out.println(b[startI - 1][startJ - 1] + "+...+"
                 + b[endI - 1][endJ - 1] + "=" + sum);
    }
}

运行结果:

开始列:

1

结束列:

2

开始行:

1

结束行:

39

开始列1结束列2开始行1结束行39和为:

60441

 

   这次的作业看似很复杂,其实做起来更麻烦,起初毫无头绪,但是经过多天和同学们交流之后终于有了起色,花了多天的研究跟思考,终于打破了我内心的恐惧,写了一点点东西,虽然我知道并不好,但我会继续努力。(ps:由于电脑问题,插图比较麻烦,所以就纯手打了,请老师务必见谅)

 

posted @ 2015-09-23 12:34  魔鬼筋肉人  阅读(140)  评论(1编辑  收藏  举报