Code training and code training for different types of problems

Source: Internet
Author: User

Code training and code training for different types of problems

Input the month on the keyboard and output the corresponding season
Case penetration:
The essence is that there is no break in the case statement body.
If there is no break, it will directly go to the statement body in the next case and continue to execute until a break exists.

1 public static void main (String [] args) {2 // TODO Auto-generated method stub 3 // use the keyboard to get the keyboard entry 4 then SC = new then (System. in); 5 System. out. println ("enter a number"); 6 int num = SC. nextInt (); 7 8 switch (num) {9 case System. out. println ("Winter"); 13 break; 14 case System. out. println ("c quarter"); 18 break; 19 case System. out. println ("Summer"); 23 break; 24 case System. out. println ("Autumn"); 28 break; 29 30 default: 31 System. out. println ("heheh"); 32 break; 33} 34}

Print all the input numbers in 5 digits
Range: 10000 ~ 100000
Condition: single digit = ten thousand digits & ten digits = thousand digits

1 public static void main (String [] args) {2 3 for (int I = 10000; I <100000; I ++) {4 // calculate each digit 5 int g = I % 10; 6 int s = I/10% 10; 7 // int B = I/10/10% 10; 8 int q = I/10/10/10% 10; 9 int w = I/10/10/10/10% 10; 10 // condition judge 11 if (g = w & s = q) {12 System. out. println (I); 13} 14} 15 16}

Array element exchange
Original array int [] arr = {10, 20, 30, 40, 50 };
Output arr = {50, 40, 30, 20, 10 };

Ideas:
Define two variables, I j, to represent the index values of the preceding and subsequent elements, respectively.
When I <j is used, elements and moving index values are repeatedly exchanged.

1 public static void main (String [] args) {2 // TODO Auto-generated method stub 3 int [] arr = {10, 20, 30, 40, 50 }; 4 // define two variables: I j 5 int I = 0; 6 int j = arr. length-1; 7 // repeatedly swap and move ij 8 while (I <j) {9 // swap 10 int tmp = arr [j]; // store the values of subsequent elements in the Temporary Variable 11 arr [j] = arr [I]; // assign the preceding element value to 12 arr [I] = tmp; // assign the values of the elements after the temporary variables to the previous element 13 14 // move the index value 15 I ++; 16 j --; 17}

Fibonacci
A rabbit was born every month from 3rd months after birth. The rabbit grew to every month after the third month.
Another rabbit was born. If the rabbit was not dead, how many rabbits were there in 20th months?

1 public static void main (String [] args) {2 // TODO Auto-generated method stub 3 int [] arr = new int [20]; 4 5 arr [0] = 1; 6 arr [1] = 1; 7 8 for (int I = 2; I <arr. length; I ++) {9 arr [I] = arr [I-1] + arr [I-2]; 10} 11 12 System. out. println ("Number of Rabbits:" + arr [19]); 13}

 

Related Article

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.