Several common loops in Java

Source: Internet
Author: User

Multiple if_else:

Package Com.dengchaoqun.ht;public class Double_for02 {/** *  * * Print multiplication table */public static void Main (string[] args) {for (int i = 1; I < 10; i++) {for (int j = 1; J <= I; j + +) {int a = i * j; System.out.print (i + "*" + j + "=" + A + "\ T");} System.out.println ();}}}

  

Switch_case:

Package Com.dengchaoqun.ht;import Java.util.scanner;public class Leap_yearorcommon_year {public static void main ( String[] args) {Scanner scan=new Scanner (system.in); int year;int month;//used to enter month SYSTEM.OUT.PRINTLN ("Enter year:"); year= Scan.nextint (); System.out.println ("Please enter month:"); Month=scan.nextint ();//Determine if a leap year if ((year%4==0&&year%100!=0) | | (year%400==0)) {System.out.println (year+) is a leap year! ");} ELSE{SYSTEM.OUT.PRINTLN (year+ "Year is common year! ");} Days of the output month switch (month)   //switch-case statement The number of days in the output month {case 1:case 3:case 5:case 7:case 8:case 10:case 12: SYSTEM.OUT.PRINTLN (year+ "year" +month+ "month is 31 days! break;//judge whether it is a leap year February or common year February case 2:if ((year%4==0&&year%100!=0) | | (year%400==0)) {System.out.println (year+ "year" +month+ "month is 29 days!) "); break;} ELSE{SYSTEM.OUT.PRINTLN (year+ "year" +month+ "month is 28 days! "); break;} Case 4:case 6:case 9:case 11:system.out.println (year+ "year" +month+ "month is 30 days! Break;default:system.out.println ("Please enter the correct year and month!") ");} Scan.close ();}}

  

While

Package Com.dengchaoqun.ht;public class While {/** *  * * uses the while single loop for 1! +2! +...+7! */public static void Main (string[] args) {//TODO auto-generated method stub int I=1;int j=1;int Sum=0;while (i<8) {j=i*j;sum+=j;i++;} SYSTEM.OUT.PRINTLN ("1! +2! +...+7! = "+sum);}}

  

Do_while:

Package Com.dengchaoqun.ht;import Java.util.scanner;public class Do_while_02 {public static void main (string[] args) {// TODO Auto-generated method stub Scanner scan = new Scanner (system.in);d o {int n = 0; System.out.println ("1. Registration"); System.out.println ("2. Login"); System.out.println ("0. Exit"); SYSTEM.OUT.PRINTLN ("Select Input:"), n = Scan.nextint (), if (n = = 0) {System.out.println ("You chose to exit"); break;} else if (n = = 1) {Syste M.OUT.PRINTLN ("You have chosen to login");} else if (n = = 2) {System.out.println ("You chose to exit");} else {System.out.println ("You have selected the error");}} while (true); Scan.close ();}}

  

For

Package Com.dengchaoqun.ht;public class Double_for02 {/** *  * * Print multiplication table */public static void Main (string[] args) {for (int i = 1; I < 10; i++) {for (int j = 1; J <= I; j + +) {int a = i * j; System.out.print (i + "*" + j + "=" + A + "\ T");} System.out.println ();}}}

  

Several common loops in Java

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.