java--Cycle Applications

Source: Internet
Author: User
Tags define function


Cycle PracticeExercise one: Calculate1~100the odd and the arithmetic between
/* Calculate the odd sum between 1~100:1, you need to define a variable to initialize and record and sum??????????? 2, calculate the number between 1~100 need to use a For loop to control the number of additions??????????? 3, need to judge the 1~100 between the odd??????????? 4, the odd sum is added, sum?=?sum?+?i??????????? 5. Print out the value */public?class after the loop is added? looptest{????????? Public?static?void?main (String[]?args) {??????????????????? Define a variable to record and??????????????????? int?sum?=?0;??????????????????? Use the For loop to traverse the number between 1~100??????????????????? for (int?i=1;i<=100;i++) {???????????????????????????? Judge the number of 1~100 between what is odd???????????????????????????? if (i%2==1) {????????????????????????????????????? Add the odd values after the judgment????????????????????????????????????? Sum?+=?i,????????????????????????????}???????????????????}??????????????????? Print out the last cumulative and??????????????????? SYSTEM.OUT.PRINTLN (sum);?????????}}

Operation Result:

Exercise two: Calculate the number of daffodils: three digits:100~999, such as1*1*1+2*2*2+3*3*3=123
/* Requirements: Find daffodils number: Three digits: 100~999, such as 1*1*1+2*2*2+3*3*3=123, find out the number of ideas that have this characteristic: 1, define three variables to record single digit, 10 bit, the number on the hundred??????????? 2. Use a For loop to traverse the number between 100~999??????????? 3, the number of split, need to use the division, modulo operation??????????? 4, to determine the number of bits, 10 bits, the sum of the hundred three number of cubic meters is the figure itself, if this number is the number of daffodils??????????? 5, print out these number */public?class? looptest_1{????????? Public?static?void?main (String[]?args) {??????????????????? Defines a single-digit variable??????????????????? int?ge?=?0;??????????????????? Defines a 10-digit variable??????????????????? int?shi?=?0;??????????????????? A variable that defines the number of hundred??????????????????? int?bai?=?0;??????????????????? Use the For loop to traverse the number between 100~999??????????????????? for (int?i=100;i<=999;i++) {???????????????????????????? Split the number???????????????????????????? on the Hundred bai?=?i?/?100;???????????????????????????? Splits the number on the 10-bit???????????????????????????? shi?=?i?/?10?%?10;???????????????????????????? Split single-digit???????????????????????????? ge?=?i?%?10;???????????????????????????? Determine if the number meets the requirements such as 1*1*1+2*2*2+3*3*3=123???????????????????????????? if (ge*ge*ge+shi*shi*shi+bai*bai*bai?==?i) {????????????????????????????????????? System.out.println (i);????????????????????????????}???????????????????}?????????}} 

Operation Result:

Exercise three: Print out uppercase and lowercase letters
/* Requirements: print uppercase and lowercase ideas: print with an ASCII-encoded table????????? 1. Define two variables to record the letters A and a????????? 2. Use the For loop to control the number of prints????????? 3, print out the number corresponding to the letter????????? 4, every cycle of this character to be added once */public?class? looptest_2{????????? Public?static?void?main (String[]?args) {??????????????????? Define two variables to record the letters A and a??????????????????? Char?daxie?=? ' A ';??????????????????? Char?xiaoxie?=? ' A ';??????????????????? Use the For loop to control the number of times you print??????????????????? for (int?i=0;i<26;i++) {???????????????????????????? System.out.println (xiaoxie+ "??") +daxie);???????????????????????????? xiaoxie++;???????????????????????????? daxie++;???????????????????}?????????}}
Exercise four: Print 99 multiplication Table
/* Requirements: Print 99 multiplication table idea: 1, 99 the multiplication table needs nine rows, each column plus an operation, need for loop nesting to implement??????????? 2, the outer loop control the number of rows, altogether nine lines, for (int?i=1;i<=9;i++)??????????? 3, the inner loop control the number of each row, such as 1*2=2,2*2=4 can be used to write a loop for (int?j=1;j<=2;j++)???????????????????? Its first number is gradually increasing, you can find that the outer loop of the variable i is also gradually increasing, the second number every time???????????????????? After the loop is constant, so you can define the first number as the variable I, the second is the cyclic variable J, they two??????????????????? By multiplying, the inner loop can be written as a for (int?j=1;j<=i;j++)??????????? 4, using the output statement to print out the loop effect, System.out.print (j+ "*" +i+ "=" +j*i); */public?class? looptest_3{????????? Public?static?void?main (String[]?args) {??????????????????? Print99 (8);?????????}????????? Public?static?void?print99 (int?k) {??????????????????? The For loop nesting is traversed, and the outer loop controls the number of rows??????????????????? for (int?i=1;i<=k;i++) {???????????????????????????? The inner loop controls the number of rows per row???????????????????????????? for (int?j=1;j<=i;j++) {????????????????????????????????????? The effect of multiplying the output two numbers????????????????????????????????????? System.out.print (j+ "*" +i+ "=" +j*i+ "???");????????????????????????????}???????????????????????????? Wraps each time the inner loop ends???????????????????????????? System.out.println ();???????????????????}?????????}} 
CharTypes andInttypes can be converted to each other
Can the/*char type convert */public?class to int type? charintdemo{????????? Public?static?void?main (String[]?args) {??????????????????? Char?c?=? ' A ';??????????????????? int?i?=?c?+?1;??????????????????? System.out.println (i);??????????????????? Char?c1?=? (char) 6;??????????????????? SYSTEM.OUT.PRINTLN (C1);?????????}}

Operation Result:

Array method Exercises (writes function into the method)

Exercise One: array traversal printing, such as printing [55,66,77,88,99]

/* Requirements: Print arrays [55,66,77,88,99] Ideas: 1, define the method, Ergodic (), return value type: void, parameter list: Give me an array of arr??????????? 2. Print the left parenthesis of the array format first [, System.out.print ("[");??????????? 3. Use the For loop to iterate through the array, for (int?i=0;i<arr.length;i++)??????????? 4. Determine if the array is traversed to the last element if (i==arr.length-1),???????????????????? Print out the last element without a comma System.out.println (arr[i]+ "]");???????????????????? Otherwise print other elements without a newline plus comma System.out.print (arr[i]+ ",");??????????? 5. Define this array in the main method, and call this array */public?class? arraymethodtest{????????? Public?static?void?main (String[]?args) {??????????????????? 5. Define this array in the main method and call this array??????????????????? Int[]?arr?=? {55,66,77,88,99};??????????????????? Ergodic (arr);?????????}????????? 1, define the method????????? Public?static?void?ergodic (Int[]?arr) {??????????????????? 2. Print the left parenthesis of the array format first [??????????????????? System.out.print ("[");??????????????????? 3. Use the For loop to iterate through the array??????????????????? for (int?i=0;i<arr.length;i++) {???????????????????????????? 4. Determine if the array is traversed to the last element???????????????????????????? if (i==arr.length-1) {????????????????????????????????????? System.out.println (arr[i]+ "]");????????????????????????????} else{????????????????????????????????????? System.out.print (arr[i]+ ",");????????????????????????????}???????????????????}?????????}}
practice one: inverse of the array (reverse-displace the elements of the array)
/* Requirements: Inverse of the array (reverse permutation of elements of the array): 1. Define function within method, displace (), return value type: void, parameter list: Give me an array Int[]?arr??????????? 2. Use the For loop to traverse the array, for (int?min?=?arr[0],int?max?=?arr[arr.length-1];min?>?min;min++,max--)??????????? 3, to replace the data, you need to use a third-party variable to record one of the value of the corner??????????? 4, define the function of printing array??????????? 5, define an array in the main method, call the method */public?class? arraymethodtest_1{????????? Public?static?void?main (String[]?args) {??????????????????? Int[]?arr?=? {1,2,3,4,5};??????????????????? Displace (arr);??????????????????? Ergodic (arr);?????????}????????? 1, the function is defined within the method, displace ()????????? Public?static?void?displace (Int[]?arr) {??????????????????? 2. Use the For loop to iterate through the array??????????????????? for (int?min?=?0,max?=?arr.length-1;max?>?min;min++,max--) {???????????????????????????? 3, to replace the data, you need to use a third-party variable to record one of the value of the corner???????????????????????????? Int?temp?=?arr[min];???????????????????????????? Arr[min]?=?arr[max];???????????????????????????? Arr[max]?=?temp,???????????????????}?????????}????????? 4, define the function of printing array????????? 1, define the method????????? Public?static?void?ergodic (Int[]?arr) {??????????????????? 2. Print the left parenthesis of the array format first [??????????????????? System.out.print ("[");??????????????????? 3. Use the For loop to iterate through the array??????????????????? for (int?i=0;i<arr.length;i++) {???????????????????????????? 4. Determine if the array is traversed to the last element???????????????????????????? if (i==arr.length-1) {????????????????????????????????????? System.out.println (arr[i]+ "]");????????????????????????????} else{????????????????????????????????????? System.out.print (arr[i]+ ",");????????????????????????????}???????????????????}?????????}}
exercise two: Array selection sort
/* Requirements: Array selection sorting ideas: 1, define a method Selectsort (), return value type: void, parameter list: array Int[]?arr??????????? 2, the use of circular nesting, the array to traverse, the outer loop to walk once for (int?i=0;i<arr.length.i++),?????????????? The inner layer loops through for (int?j?=?i+1;j<arr.length;j++), then it can be understood as the first angle to go once,?????????????? Other corner marks go through??????????? 3, the size of the judgment, the angle of the small point is placed on the index 0 of the corner superscript if (Arr[i]>arr[j]), the position of the replacement??????????? 4, to an array in the Main method call this method */public?class? arraymethodtest_2{????????? Public?static?void?main (String[]?args) {??????????????????? 4. Call this method in the main method for an array??????????????????? Int[]?arr?=? {45,78,69,35,85,25};??????????????????? Selectsort (arr);??????????????????? Ergodic (arr);?????????}????????? 1. Define a method Selectsort ()????????? Public?static?void?selectsort (Int[]?arr) {??????????????????? 2, the use of circular nesting method, the array to traverse??????????????????? for (int?i=0;i<arr.length;i++) {???????????????????????????? for (int?j?=?i+1;j<arr.length;j++) {????????????????????????????????????? 3. Judge the size????????????????????????????????????? if (Arr[i]>arr[j]) {??????????????????????????????????????????????? The replacement of the position??????????????????????????????????????????????? Int?temp?=?arr[i];??????????????????????????????????????????????? ARR[I]?=?ARR[J];?????????????????????????????????????????????? Arr[j]?=?temp,?????????????????????????????????????}????????????????????????????}???????????????????}?????????}????????? The ability to print arrays????????? 1, define the method????????? Public?static?void?ergodic (Int[]?arr) {??????????????????? 2. Print the left parenthesis of the array format first [??????????????????? System.out.print ("[");??????????????????? 3. Use the For loop to iterate through the array??????????????????? for (int?i=0;i<arr.length;i++) {???????????????????????????? 4. Determine if the array is traversed to the last element???????????????????????????? if (i==arr.length-1) {????????????????????????????????????? System.out.println (arr[i]+ "]");????????????????????????????} else{????????????????????????????????????? System.out.print (arr[i]+ ",");????????????????????????????}???????????????????}?????????}}
exercise three: bubble sort of array
/* Requirements: array of bubble sorting ideas: 1, define a method Bubblesort (), return value type, void, parameter list Int[]?arr??????????? 2. Use the For loop to iterate through the array, the outer loop controls the number of cycles for (int?i?=?0;i<arr.length;i++)???????????????????? The bubbling sort is a comparison of two adjacent elements to sort the inner loop that controls its two adjacent two elements in a comparison loop, if J's?????????????? The index is 0, and the index of the neighboring element is the index of the 0+1,j each cycle must be compared from the 0 index, and the length control of the inner loop is arr.length-i-1,?????????????? for (int?j=0;j<arr.length-i-1;j++)??????????? 3, the size of the element to determine if (arr[j]>arr[j+1]), the displacement of the position??????????? 4, define an array in the main method, call the method */public?class? arraymethodtest_3{????????? Public?static?void?main (String[]?args) {??????????????????? ? 4, define an array in the main method, call the method??????????????????? Int[]?arr?=? {15,2,78,65,48,95,48};??????????????????? Bubblesort (arr);??????????????????? Ergodic (arr);?????????}????????? 1. Define a method Bubblesort ()????????? Public?static?void?bubblesort (Int[]?arr) {??????????????????? 2. Use the For loop to iterate through the array??????????????????? for (int?i?=?0;i<arr.length;i++) {???????????????????????????? for (int?j=0;j<arr.length-i-1;j++) {????????????????????????????????????? 3, the size of the elements to Judge????????????????????????????????????? if (arr[j]>arr[j+1]){??????????????????????????????????????????????? Position Replacement??????????????????????????????????????????????? INT?TEMP?=?ARR[J];??????????????????????????????????????????????? ARR[J]?=?ARR[J+1];??????????????????????????????????????????????? Arr[j+1]?=?temp,?????????????????????????????????????}????????????????????????????}???????????????????}?????????}???????? ?//The function of printing arrays????????? 1, define the method????????? Public?static?void?ergodic (Int[]?arr) {??????????????????? 2. Print the left parenthesis of the array format first [??????????????????? System.out.print ("[");??????????????????? 3. Use the For loop to iterate through the array??????????????????? for (int?i=0;i<arr.length;i++) {???????????????????????????? 4. Determine if the array is traversed to the last element???????????????????????????? if (i==arr.length-1) {????????????????????????????????????? System.out.println (arr[i]+ "]");????????????????????????????} else{????????????????????????????????????? System.out.print (arr[i]+ ",");????????????????????????????}???????????????????}?????????}}
exercise four: Binary lookup method (premise: Must be an ordered array)
/* Binary Find Method: 1, define a method BinarySearch (), return value type: int, parameter list: Int[]?arr, number to find Int?key??????????? 2. Define three variables (pointers) int?min?=?0,int?max?=?arr.length-1,int?mid?=?0??????????? 3, the Loop lookup, small pointer is less than equal to the large pointer while (MIN&LT;=MAX) on the loop lookup, the small pointer is larger than the large pointer to indicate that the array did not find this number????????????? Returns-1??????????? 4, to judge:????????????????????? Do binary operation mid?=? (Min+max)/2????????????????????? If Key>arr[mid],min?=?mid+1????????????????????? If Key<arr[mid],max?=?mid-1????????????????????? Until Mid==min or Max, indicating that the value has been found, returns the index mid??????????? 5, define the array in the main method, call the method */public?class? arraymethodtest_4{????????? Public?static?void?main (String[]?args) {??????????????????? 5, define the array in the main method, call the method??????????????????? Int[]?arr?=? {1,2,3,4,5,6,7,8,9};??????????????????? Int?num?=?binarysearch (arr,5);??????????????????? SYSTEM.OUT.PRINTLN (num);?????????}????????? 1. Define a method BinarySearch ()????????? Public?static?int?binarysearch (int[]?arr,int?key) {??????????????????? 2. Define three variables (pointers)??????????????????? Small Hands??????????????????? int?min?=?0;??????????????????? Intermediate pointer??????????????????? int?mid?=?0;??????????????????? Large pointer??????????????????? int?max?=?arr.length-1;??????????????????? 3, the Loop lookup, small pointer is less than equal to the large pointer while (MIN&LT;=MAX) on the loop lookup, the small pointer is larger than the large pointer that the array does not find this number returned-1??????????????????? while (Min<=max) {???????????????????????????? ? 4, to Judge???????????????????????????? Mid?=? (Min+max)/2;???????????????????????????? if (Key<arr[mid]) {????????????????????????????????????? Max?=?mid?-? 1;????????????????????????????} Else?if (Key>arr[mid]) {????????????????????????????????????? min?=?mid?+?1;????????????????????????????} else{????????????????????????????????????? Return?mid,????????????????????????????}???????????????????}??????????????????? return?-1;?????????}}


java--Cycle Applications

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.