1. 设定一个类(即zuoye),实现对一维数组的指定范围内的元素相加运算
package abc;
import java.util.Scanner;//手工输入
public class zuoye {
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};
int sum=0;
Scanner ce = new Scanner(System.in);
System.out.println("开头数:");
int start = ce.nextInt() ;
System.out.println("结尾数:");
int end =ce.nextInt();
System.out.println("决定范围");
System.out.println("第"+start+"个元素到第"+end+"个元素");
for(int i=start-1;i<=end-1;i++)
sum+=arr1[i]; //循环相加
System.out.println("相加运算:");
System.out.println(sum);
}
}
第一个类可实现对一维数组的指定范围内的元素相加。
运行结果如下:
2.设计一个类(即zuoye1 ),可以实现对二维数组的指定范围内的元素相加运算。
代码如下:
package abc;
import java.util.Scanner;//依旧手打~~
public class zuoye1 {
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},{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 ce = new Scanner(System.in);
System.out.println("开始行");
int startH = ce.nextInt();
System.out.println("结束行");
int endH =ce.nextInt();
System.out.println("开始列?");//上限100列
int startL = ce.nextInt();
System.out.println("结束列");
int endL =ce.nextInt();
System.out.println("决定范围");
System.out.println("第"+startH+"行第"+startL+"列 到 第"+endH+"行第"+endL+"列");//最后输出的形式
for(int i=startH-1;i<=endH-1;i++)
{
for(int j=startL-1;j<=endL-1;j++)//for语句
{
sum+=a[i][j];
}
}
System.out.println(sum);
}
}
运行结果如下
3. 设计一个类(即zuoye2),可以实现对三维数组的指定范围内的元素相加运算。
代码如下:
package abc;
import java.util.Scanner;
public class zuoye2 {
public static void main(String[]args)
{
int sum=0;
int[][][] a = 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}}
};//这边的三位数组定义是参考老师同学的建议。设定y=0.
Scanner ce = new Scanner(System.in) ;
System.out.println("x开始");//1~3
int startH = ce.nextInt() ;
System.out.println("x结束");
int endH =ce.nextInt();
System.out.println("y开始");//1~100
int startZ = ce.nextInt() ;
System.out.println("y结束");
int endZ =ce.nextInt();
System.out.println("z开始");//只有1
int startC = ce.nextInt() ;
System.out.println("z结束");
int endC =ce.nextInt();
for(int i=startH-1;i<=endH-1;i++)
{
for(int j=startC-1;j<=endC-1;j++)
{
for(int q=startZ-1;q<=endZ-1;q++)
{
sum+=a[i][j][q];
}
}
}
System.out.println(sum);
}
}
运行结果如下:
第三个类即可实现对三维数组的指定范围内的元素相加。
最后总结:首先感谢老师同学对我种种问题的答疑,由于是在短时间里弄好的程序,可能还有些瑕疵,这里先和老师说声对不起。我试着把测试数据放在文件中,但是可能是由于我电脑的Word有问题,虽然能选择性粘贴,但是却不能用无格式文本显示出来,这里我也找张振渊探讨过,最后实在没办法还是不用文件形式表达。
关于程序检查也是很让人头大,由于基础不是很扎实,每次都要是个半天才能找到错误,甚至重新打一遍代码,想起来还真是笨啊,明明可以问大神,还是多靠自己好点吧。但是这样也让我对程序的代码了解得更深入了。(再次感谢答疑的同学- -打错字母让人找了半天)。
最后总结是没有写不出的代码,只是没有一颗敢尝试的心!谢谢老师的提醒,我以后不会做一个纯粹的百度学生了!!