☆第二次作业

Posted on 2015-09-24 17:31  二色☆パワー  阅读(237)  评论(2编辑  收藏  举报

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

import java.util.Scanner;
public class TestA {
    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};
     int sum=0;   //sum用于存放计算总和
     Scanner se=new Scanner(System.in);            
     System.out.println("输入从第几个数字开始:");
     int st=se.nextInt() ;
     if ((st<0)||(st>100)){     //判断输入数字是否在0-100区间内
         System.out.println("输入有误");
         return;
                            }
     System.out.println("输入加到第几个数字:");
     int en=se.nextInt();
     if ((en<0)||(en>100)){       //判断输入数字是否在0-100区间内
         System.out.println("输入有误");
          return;
                             }  
     if (en<=st){             //判断第二个数是否小于等于第一个数,是则有错误
         System.out.println("第二个数不能小于等于第一个数!");
         return;
                 }
     for(int i=st-1;i<=en-1;i++)  //进行计算
         sum+=a[i];
     System.out.println("从第"+st+"个数字加到第"+en+"个数字的和为"+sum);
    }
                  }

 

运行结果如下:

 

如果输入数字小于0,或者超出了100,则显示输入有误,停止运算。

 

输入的第一个数字如果大于第二个数字,同样输入错误,停止运算。

 

 

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

import java.util.Scanner;
public class TestB {
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;         //sum用于存放计算总和        
    Scanner se=new Scanner(System.in);
    System.out.println("输入从第几行开始:");
    int hst=se.nextInt(); 
    if ((hst<0)||(hst>2))
    {
        System.out.println("请输入1-2之间的行数");
        return;
    }
    System.out.println("输入到第几行结束:");
    int hen=se.nextInt();
    if ((hen<0)||(hen>2))
    {
        System.out.println("请输入1-2之间的行数");
        return;
    }
    System.out.println("输入从第几列开始:"); 
    int lst=se.nextInt(); 
    if ((lst<0)||(lst>50))
    {
        System.out.println("请输入1-50之间的列数");
        return;
    }
    System.out.println("输入到第几列结束:");
    int len=se.nextInt();
    if ((len<0)||(len>50))
    {
        System.out.println("请输入1-50之间的列数");
        return;
    }
    for(int i=hst-1;i<=hen-1;i++)//用嵌套循环计算总和
    {
        for(int j=lst-1;j<=len-1;j++)
        {
            sum+=b[i][j];
        }
    }
    if ((hst>hen)||(lst>len)){
            System.out.println("第二个数大于第一个数,有误!");
            return;
                             }
    System.out.println("第"+hst+"行第"+lst+"列 到 第"+hen+"行第"+len+"列的相加结果为"+sum);//输出结果
                                       }
                      }

 

 

运行结果:

 

输入错误结果:

 

 

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

 

import java.util.Scanner;
public class TestC {
public static void main(String[]args)
{
    int sum=0;
    int[][][] c=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 xst=se.nextInt() ;
    if ((xst<0)||(xst>3)){
        System.out.println("请输入1-3之间的数字");
        return;
                         }
    System.out.println("输入x的结束数字:");
    int xen=se.nextInt();
    if ((xen<0)||(xen>3)){
        System.out.println("请输入1-3之间的数字");
        return;
                         }
    if (xst>xen){
        System.out.println("第二个数大于第一个数,有误!");
        return;
                 }
    System.out.println("输入y的开始数字:");
    int yst= se.nextInt() ;
    if ((yst<0)||(yst>100)){
        System.out.println("请输入1-100之间的数字");
        return;
                            }
    System.out.println("输入y的结束数字:");
    int yen=se.nextInt();
    if ((yen<0)||(yen>100)){
        System.out.println("请输入1-100之间的数字");
        return;
                            }
    if (yst>yen){
        System.out.println("第二个数大于第一个数,有误!");
        return;
                }
    System.out.println("输入z的开始数字:");
    int zst= se.nextInt() ;
    if ((zst<0)||(zst>1)){
        System.out.println("这里z只能为1");
        return;
                         }
    System.out.println("输入z的结束数字:");
    int zen=se.nextInt();
    if ((zen<0)||(zen>1)){
        System.out.println("这里z只能为1");
        return;
                          }
    for(int i=xst-1;i<=xen-1;i++)
    {
        for(int j=zst-1;j<=zen-1;j++)
        {
            for(int k=yst-1;k<=yen-1;k++)
            {
                sum+=c[i][j][k];
            }
        }
    }
    System.out.println("计算结果为:"+sum);
}
                      }

 

运行结果:

 

输入错误结果:

 

 

 总结:

      这次的作业对我是一次很好的温习,java课结束之后其实我已经很久没有自己动手编程了,都在看别人的成品。实际上手发现,真的是生疏了,看来时不时自己动动手也是很重要的。看到二维三维数组,一开始也反应不过来,也没想到要用循环嵌套,特别去google查了下二维数组,才有了点想法。开始也不懂为什么z只能是1,后来仔细想了想,发现这是个100x3x1的图形,并不是个正方体。 久违的写了回代码,发现还是很有意思的,最后的到想要的结果的一瞬间,很开心。