Learn Java-6 from scratch. Execute operations repeatedly using loops and learn java-6

Source: Internet
Author: User

Learn Java-6 from scratch. Execute operations repeatedly using loops and learn java-6

1. Use the for loop;

2. Use the while loop;

3. Use the do-while loop;

4. Early exit loop (break, continue );

5. Name the loop.

 

Program Nines: display the product of integers 1-and 9

 1 package com.jsample; 2  3 public class Nines { 4     public static void main(String[] args){ 5         for(int dex = 1;dex <= 200; dex++){ 6             int multiple = 9 * dex; 7             System.out.println(multiple + " "); 8         } 9         System.out.println();10     }11 }
View Code

 

Output:

9

......

1800

 

Program Benchmark: view the number of times the computer calculates the square root in one minute

1 package com. jsample; 2 3 public class Benchmark {// declare Benchmark class 4 public static void main (String [] args) {// start the main () block of the program 5 long startTime = System. currentTimeMillis (); // create the variable startTime, and assign the initial value 6 long endTime = startTime + 60000 to the variable with the current time (millisecond value); // create the endTime variable, exactly one minute before startTime. 7 long index = 0; // create the loop variable index 8 while (true) {// infinite loop. You can only use break to break 9 double x = Math. sqrt (index); // calculates the square root of the index and stores it in the x variable 10 long now = System. currentTimeMillis (); // create the variable now and assign the current time to it 11 if (now> endTime) {// more than one minute, jump out of the loop 12 break; 13} 14 index ++; // auto-increment loop variable 15} 16 System. out. println (index + "loops in one minute. "); // display the number of times the square root is calculated 17} 18}
View Code

 

Output:

12880432183 loops in one minute.

 

Program Mod13: Find the first 400 divisible by 13

 1 package com.jsample; 2  3 public class Mod13 { 4     public static void main(String[] args){ 5         int index = 0; 6         for(int flag = 1; flag <= 400; index++){ 7             if(index % 13 == 0) { 8                 flag++; 9                 System.out.println(index + " ");10             }11         }12     }13 }
View Code

 

Output:

0

......

5187

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.