1 PackageWork ;2 3 ImportJava.util.Scanner;4 5 ImportJava.util.Arrays;//importing Java packages that support this program6 7 Public classwork22 {8 Public Static voidMain (string[] args) {9 int[] Array1 ={-2574,-4010,8243,771,2447,-5197,2556,8044,3314,Ten3617,6065,-2817,3131,6318,2186,-113,629,-2582,-37, One-1520,164,2055,-5936,5912,1717,5988,4781,5757,892, A-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, the-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};//define arrays and assign values to static arrays + intSum=0; -Scanner Scanner =NewScanner (system.in);//to enter data from the keyboard, use the Nextint () method of the scanner class +System.out.println ("Please enter a starting number:"); A intn=scanner.nextint (); atSystem.out.println ("Please enter an end number:"); - intt=scanner.nextint (); -System.out.println ("Number of Start:" +n+ "Terminator:" +t); - for(intI =n;i<=t;i++) {//use the For statement to make a loop addition in the starting and ending numbers. -sum+=array1 [i]; -System.out.println ("sum=" +sum); in - } to } +}
Run Results Demo:
Running the results here makes me wonder why there are multiple rows of results output. Re-read the book, and found that the loop for the statement a bit of a problem. After correction, the result of the operation is as follows:
The problem is in the loop with the For statement:
for (int i =n;i<=t;i++) { sum+ =array1 [i];} System.out.println ("sum=" +sum); for (int i =n;i<=t;i++) { sum+ =array1 [i]; System.out.println ("sum=" +sum);}
Error in the For Statement loop for a {}.
Two-dimensional arrays:
PackageWork ;ImportJava.util.Scanner; Public classWORK2A { Public Static voidMain (String[]args) {int[] array2={{-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}}; intSum=0; Scanner Scanner=NewScanner (system.in);//as with the previous program, the scanner class is required. System.out.println ("Please enter a starting number x1:");//Unlike the previous program, because it is a two-dimensional array, enter the number of two x-axis or y-axis first, then Add. intx1=Scanner.nextint (); System.out.println ("Please enter an ending number x2:");intX2=Scanner.nextint (); System.out.println ("Start number:" +x1+ "End Number:" +x2); System.out.println ("Please enter a starting number y1:");inty1=Scanner.nextint (); System.out.println ("Please enter a starting number y2:");intY2=Scanner.nextint (); System.out.println ("Start number:" +y1+ "End Number:" +y2); for(inti=x1;i<=x2;i++) for(intj=y1;j<=y2;j++) {sum+=array2[i][j];} System.out.println ("Sum=" +sum);}}
Here is the result of the demo:
Personal Experience Summary:
I think that in the code optimization, we also need to work harder, such as Java How to read data in Excel or XML files, the information found on the Internet is also the foggy. After all, my foundation is still too weak. In the process of writing this code, there are many different ways to behave. For example, the final calculation site, tried to use the FOR-IF statement. found that their understanding of the if statement is very small, so give up a variety of combinations of statement calculation, but also need a lot of review of the previous Java EE Tutorial. Only on the basis of the use of proficiency to reach a higher level of grammar. When I wrote the first program, I imported the Java.util.Arrays package at the beginning, but when I finished the whole program, the package was not used. Probably because I just copied the data in the array manually, without using the array reference or the traversal effect. So in the second program did not import this package, the final program is also running smoothly. There are a lot of small mistakes, which have been corrected one after the other. So, I think that writing code is a deliberately, not impatient to want to produce results, but to achieve the needs of my needs, the final combination is the result I want.
Software Engineering Second Job