Road day03 Big Data - The extension application and practice java cycle (while, do-while, for)

Today, I was suddenly a very awkward question to ask, the question is this: talk about scenarios java cycle .

I think a lot of people all of a sudden hear this question does not know how to answer, most people will think about what round robin format, what characteristics yes. These are wrong, later in the interview process, the interviewer might ask questions like this trap.

The correct answer: java while loop cycle included, do-while loops, for loops. After the while loop which directly determines further data manipulation, repeated operation (simple), the do-while loop is a particular operation must be re-enter the circulation operation, the loop for while loop.

Well, he did not talk much, this section to start the realization of the example code:

 

Examples of 1:99 multiplication table

Mentioned cycle, no matter what language to learn, 99 multiplication table will encounter this problem.

Two loop for controlling the output, a first control for 1-9, a second control number for each row.

 

 1 package day03;
 2 
 3 public class while02 {
 4 
 5     public static void main(String[] args) {
 6         //99乘法表
 7         int result;
 8         for(int y=1;y<10;y++) {
 9             for(int j=1;j<=y;j++) {
10                 result = y*j;
11                 System.out.print(j+"*"+y+"="+result+"\t");              
12             }
13             System.out.println();
14         }
15         
16         
17     }
18 
19 }

 

operation result:

 

 

 Examples 2: do-while conversion between Celsius and Fahrenheit (Note that this example cast type)

. 1  Package day03;
 2  
. 3  public  class exer10 {
 . 4  
. 5      public  static  void main (String [] args) {
 . 6          // do the while-= Fahrenheit Celsius. 9 * / 32 + 5.0 
. 7          a float S = 0 ;
 . 8          a float H = 0 ;
 . 9          int I =. 1 ;
 10          do {
 . 11              H = ( a float ) (S *. 9/32 + 5.0 );
 12 is              System.out.println ( "Celsius" + s + "corresponds Fahrenheit:" + H );
 13 is              S = S + 20 is;
14             i++;
15         } while (i <= 10 && s <= 250);
16         
17     }
18 
19 }

operation result:

 

 

 Example 3: Calculate the proportion of customers (taking into account how to achieve statistical entry age to calculate the final ratio)

. 1  Package day03;
 2  
. 3  Import java.util.Scanner;
 . 4  
. 5  public  class exer13 {
 . 6  
. 7      public  static  void main (String [] args) {
 . 8          // student is to calculate the proportion of customers 
. 9          a float up = 0 ;
 10          a float Down 0 = ;
 . 11          Scanner SC = new new Scanner (the System.in);
 12 is          for ( int I =. 1; I <= 10; I ++ ) {
 13 is              of System.out.print ( "Please enter" + i + "position customer Age: " );
 14             int Age = sc.nextInt ();
 15              IF (Age> 30 ) {
 16                  up + =. 1 ;
 . 17              } the else  IF (Age <30 ) {
 18 is                  Down + =. 1 ;
 . 19              }
 20 is          }
 21 is          System.out.println ( "ratio is 30 years:" Down + 10% + 10 * "%" );
 22 is          System.out.println ( "ratio is over 30:" + 10 * 10 +% up "%" );
 23 is          
24          
25  
26 is      }
 27  
28 }

operation result:

 

 

Example 4: Verify user login information (using the nested for loops and if to achieve, the main consideration how to implement and practice after entering the correct, enter the correct approach)

. 1  Package day03;
 2  
. 3  Import java.util.Scanner;
 . 4  
. 5  public  class exer15 {
 . 6  
. 7      public  static  void main (String [] args) {
 . 8          // practitioners operation - user login authentication information 
. 9          Scanner SC = new new Scanner ( the System.in);
 10          
. 11          Boolean In Flag = to true ;
 12 is          String man_name = "Xiaowang" ;
 13 is          String man_password = "123456" ;
 14          for ( int. 1 = I; I <=. 3; I ++ ) {
 15              of System.out.print ( "Please enter your name:" );
 16              String name = sc.next ();
 . 17              of System.out.print ( "Enter Password: " );
 18              String password = sc.next ();
 19              IF (name.equals (man_name) && password.equals (man_password)) {
 20                  Flag = to true ;
 21                  System.out.println ("! Welcome MyShopping system. " );
 22 is                  BREAK ;
 23 is              } the else {
 24                 System.out.println ( "! Mistake, you have" + (3-i) + " Chance!" );
 25                  System.out.println ();
 26                  Flag = false ;
 27              }
 28          }
 29          IF ( == In Flag to false ) {
 30              System.out.println ( "Sorry, you have three chances to enter error!" );
 31          }
 32          
33 is  
34 is      }
 35  
36 }

operation result:

1, the input is wrong results

 

 2. Enter once the correct result:

 

 3. Enter the second time the right:

 

 Of course, my practice is certainly more than these four, I just pick a representative for a more integrated, IT industry, we must knock yourself, there is a saying that good: the code is only logical implementation process, we must always practice . Come on!

Guess you like

Origin www.cnblogs.com/wyh-study/p/11808155.html