1. Design a class (called a) that implements the addition of elements within a specified range of one-dimensional arrays.
1 PackageYang;2 3 ImportJava.util.Scanner;//using the Scanner class to enter data from the keyboard4 5 Public classyy {6 Public Static voidMain (String[]args) {7 8 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};9 Ten intsum=0;//used for statistics and OneScanner A =NewScanner (system.in);//defines the class's object A, which receives data from the keyboard ASystem.out.println ("Where to start?") :"); - intStart =a.nextint (); -System.out.println ("Where to end?") :"); the intEnd =a.nextint (); -SYSTEM.OUT.PRINTLN ("Specified range is:" +start+ "---" +end); - for(inti=start-1;i<=end-1;i++) -Sum+=aa[i];//Loop here +System.out.println ("Arithmetic and:"); - System.out.println (sum); + } A}
The result of the operation is:
2. Design a class (which may be called B) to implement the addition of elements within a specified range of a two-dimensional array.
1 PackageYang;2 ImportJava.util.Scanner;3 Public classYy2 {4 Public Static voidMain (string[] args) {5 int[] b={6{ -2574,-4010,8243,771,2447,-5197,2556,8044,3314,3617,6065,-2817,3131,6318,2186,-113,629,-2582,-37,-1520,7164,2055,-5936,5912,1717,5988,4781,5757,892,-4394,8034,2213,-1080,-2080,5364,106,2657,566,3940,-5116,84583,1806,6555,2621,-7197,528,1626,18,1049,6243,3198,4397,-1325,9087,936,-6291,662,-178,135,-3473,-2385,9-165,1713,-7949,-4234,1138,2212,104,6968,-3632,3801,1137,-1296,-1215,4272,6223,-5922,-7723,7044,-2938,-8180,Ten1356,1159,-4022,-3713,1158,-8715,-4081,-2541,-2555,-2284,461,940,6604,-3631,3802,-2037,-4354,-1213,767}, One{2740,-4182,-5632,-2966,-1953,567,-8570,1046,2211,1572,-2503,-1899,3183,-6187,3330,3492,-464,-2104,316,8136, A470,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, the-7514,7450,-1142,-2666,-4485,-639,2121,-5298,-3805,-1686,-2520,-1680,2321,-4617,-1961,2076,7309} - }; - -Scanner B1 =NewScanner (system.in); +System.out.println ("Horizontal axis start:");//consider a two-dimensional array as a axis - intStarta =b1.nextint (); +SYSTEM.OUT.PRINTLN ("Axis end:"); A intENDA =b1.nextint (); atSystem.out.println ("Vertical axis start:"); - intSTARTD =b1.nextint (); -SYSTEM.OUT.PRINTLN ("Axis end:"); - intENDD =b1.nextint (); -System.out.println ("Specify a range of:"); -System.out.println ("Horizontal" +starta+ "---" +ENDA); inSystem.out.println ("Vertical axis" +startd+ "---" +endd); - to intJ=startd-1; + intSum=0; - for(inti=starta-1;i<enda-1; i++)//summing a nested statement with a for While loop the { * while(j<endd-1) $ {Panax NotoginsengJ + +; -sum=sum+B[i][j]; the } + } A theSystem.out.println ("Sum:" +sum); + - } $}
The result of the operation is:
This homework can be counted after the vicissitudes _ (: Зゝ∠) _, the array although previously learned, but also a vague concept, and did not want to make it clear words, but now in order to write a good job, to go back and forth the book over several times, but also to ask the students have done well, if the previous study I can have this attitude, Maybe it's not so painful to write code now. But also because of this homework, let me learn a lot of knowledge, like using the scanner class to receive input from the keyboard information. The scanner class can not only read data from the keyboard, but also read from other data sources. In this code writing, I found that I can easily miss Curly braces, semicolons, and so on some small problems, often because these problems lead to most of the code errors, but also often can not find the cause of the error _ (: Зゝ∠) _, a line of code to find down, found that because of missing small symbols, I want to say that they really careless can
I think my code is still a lot of problems, one-dimensional two-dimensional reluctantly written out, but three-dimensional array of writing feel a little bit, wrong, and often get rid of this error will appear new errors, there are several changes without error, a run compiler error, because no run out of results, I did not paste the code of the three-dimensional array, I will study later, the code is perfect and run out, then publish the essay to release him. Then, learning really is to review review, because a period of time not to see, a lot of things will forget _ (: Зゝ∠) _. I will be in the next time to study hard, while learning new content at the same time to review the previous learning knowledge! Perfect yourself! (?•?? •?)??
2nd Assignment (personal project Practice)