Java Process Control

Source: Internet
Author: User

The Java Process Control syntax is similar to C/C + +, as well as If...else, while, Do...while, for, Switch...case, and so on, where the specific syntax is no longer described, just an example.

Output 99 multiplication table (upper right triangle):

1. public class Demo {

2. public static void Main (string[] args) {

3. int I, J;

4. for (I=1; i<=9; i++) {

5. for (j=1; j<=9; J + +) {

6. if (j<i) {< p= "" >

7.//print eight spaces, remove the space is the upper left triangle

8. System.out.print ("");

9.}else{

System.out.printf ("%d*%d=%2d", I, J, I*j);

11.}

12.}

System.out.print ("\ n");

14.}

15.}

16.}

Operation Result:

1*1= 1 1*2= 2 1*3= 3 1*4= 4 1*5= 5 1*6= 6 1*7= 7 1*8= 8 1*9= 9

2*2= 4 2*3= 6 2*4= 8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18

3*3= 9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27

4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36

5*5=25 5*6=30 5*7=35 5*8=40 5*9=45

6*6=36 6*7=42 6*8=48 6*9=54

7*7=49 7*8=56 7*9=63

8*8=64 8*9=72

9*9=81

There is also a printf () statement in Java that controls the output format, but it is not commonly used in real-world development, because data output to the console rarely requires a strict format, and General println () and print () are sufficient.

println () outputs the content and wraps, and print () does not wrap.

For example, how many days are there in a certain month of a year:

1. Import java.util.*;

2. public class Demo {

3. public static void Main (string[] args) {

4. int days = 0;

5.

6.//Get user input

7. Scanner sc = new Scanner (system.in);

8. System.out.print ("Enter year:");

9. int year = Sc.nextint ();

System.out.print ("Enter month:");

one. Int month = Sc.nextint ();

12.

Switch (month) {

Case 1:

Case 3:

Case 5:

Case 7:

Case 8:

Case 10:

Case 12:

days=31;

break;

Case 4:

Case 6:

Case 9:

Case 11:

days=30;

break;

Case 2:

30.//Judging leap year

if (year%4==0 && year%100!=0 | | year%400==0)

days=29;

. else

days=28;

break;

Default:

Panax Notoginseng System.out.println ("Month input error!");

System.exit (0); Force End Program

39.}

System.out.printf ("Day:%d\n", days);

41.}

42.}

Operation Result:

Input year: 2014

Input month: 02

Days: 28

There is no scanf () statement in Java like the C language, getting input from the console is a bit cumbersome, I recommend using the Scanner class, the specific syntax please see the API for yourselves. (Edit: Lelinpeng Source: Network)

Java Process Control

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.