第二次作业 个人项目实践

1.一维数组代码如下:
package
s008;
import java.util.Scanner; //利用scanner类从键盘导入数据

publicclass A

{ public static void main(String[] args)
{
int[] arr1={-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
};
Scanner a = new Scanner(System.in);  //实例化一个对象,用a调用方法
         System.out.println("起始位");
           int start = a.nextInt() ; //利用nextInt()从键盘导入数据
         System.out.println("结束位:");
           int end =a.nextInt();
         System.out.println("指定的范围:"+start+"---"+end);
         int i=start-1; //因为a[i]表示数i-1,因此i=start-1
         int sum=0;
         while(i<end-1)    
          {
             sum=sum+arr1[i];
                 i++;
                 }
                  System.out.println("总和为:"+sum);
                }

            }

代码运行结果:
第二次作业 个人项目实践_第1张图片

 

 

2.二维数组代码如下:
package
s008; import java.util.Scanner; public class B { public static void main(String[] args) { int[][] arr2={
{ -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}
}; Scanner b
= new Scanner(System.in); System.out.println("起始行:");//将二维数组看做一个平面图,利用行A和列D来排列 int startA = b.nextInt(); System.out.println("终止行:"); int endA =b.nextInt(); System.out.println("起始列:"); int startD = b.nextInt(); System.out.println("终止列:"); int endD =b.nextInt(); System.out.println("计算的范围:"); System.out.println("行位"+startA+"---"+endA);//行取值为1-2 System.out.println("列位"+startD+"---"+endD);//列取值为1-100 int j=startD-1; int sum=0; for(int i=startA-1;i<endA-1; i++) //利用for while循环嵌套语句进行求和 { while(j<endD-1) { j++; sum=sum+arr2[i][j]; } } System.out.println("总和为:"+sum); } }


代码运行如下:
第二次作业 个人项目实践_第2张图片

 

3.三维数组代码:
package s008;
import java.util.Scanner; public class C { public static void main(String[]args) { int[][][] arr3 = 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 c = new Scanner(System.in) ; //将三维数组看作立体图,利用x,y,z坐标进行模仿 System.out.println("起始横坐标:");//x取值1-3 int startX = c.nextInt() ; System.out.println("终止横坐标:"); int endX =c.nextInt(); System.out.println("开始纵坐标:");//y取值1 int startY = c.nextInt() ; System.out.println("终止纵坐标:"); int endY =c.nextInt(); System.out.println("开始垂直轴:");//z取值1-100 int startZ = c.nextInt() ; System.out.println("终止垂直轴:"); int endZ =c.nextInt(); System.out.println("运算范围:"); System.out.println("横坐标范围:"+startX+"---"+endX); System.out.println("纵坐标范围:"+startY+"---"+endY); System.out.println("垂直坐标范围:"+startZ+"---"+endZ); int sum=0; for(int i=startX-1;i<=endX-1;i++) { for(int j=startY-1;j<=endY-1;j++) { for(int m=startZ-1;m<=endZ-1;m++) { sum=sum+arr3[i][j][m]; } } } System.out.println("总和"+sum); } }

代码运行如下:
第二次作业 个人项目实践_第3张图片

 

4.小结:

  一开始看到这个题目时感觉无从下手,后来看到老师给的数据和建议想到C++中关于有具体的介绍,Java中也有部分涉及。但是开始编写时还是觉得自己力不从心,深深感觉到大一大二只是应付考试,没有认真去学,所以深感自己知识和技能上的缺陷,还需要积累学习。幸好有老师不断地提点,还有其他同学的程序,自己才能写出一点。

  数组的数据是老师给的,虽然老师建议将数据导入程序中但是对网上的一些导入技巧不太理解,所以学习其他同学手动导入数据。在这次编写中,参考了其他同学运用了Scanner类的nextInt()方法,当然其中也遇到一些错误。例如在一维数组中利用while语句求和时,忘记写i++导致结果无法求出;在利用for,while嵌套语句对数组求和中对i进行条件约束时,应该些为i<end-1不然会出现范围超过的提示,因为数组的第一个数是从a0开始数的。尤其是在三维数组中在模仿其他同学时发现自己的运算范围依然超出数组,后来发现自己的数组定义y是垂直坐标的范围是1。虽然自己的编写很差,参考了很多资料,但是能够将程序运行出来,还是觉得很值得。

你可能感兴趣的:(第二次作业 个人项目实践)