JAVA Getting Started programming

Source: Internet
Author: User

1. Input and output

When a Scanner is created through new Scanner (system.in), the console waits for input until the hit key is finished and the input is passed to Scanner as the scanned object. If you want to get the input, you only need to call Scanner's Nextline () method. The code is as follows:

Scanner input = new Scanner (system.in);
SYSTEM.OUT.PRINTLN ("Hint: enter");
int a = Input.nextint ();

2. Basic statement

If statement: if (Boolean expression) {

If the Boolean expression is ture, the statement executed

}

Example:

Scanner input = new Scanner (system.in);
System.out.println ("Enter a number");
int a = Input.nextint ();
if (a > 10) {
System.out.println (a);
}
If Else statement: if (Boolean expression) {

Executes when the Boolean expression is ture

}else{

Executed when the Boolean expression is wai false

}

Example:

Scanner input = new Scanner (system.in);
System.out.println ("Enter a number");
int a = Input.nextint ();
if (a > 10) {
System.out.println (a);
}else{
System.out.println (a);
}

Switch statement:

Switch (variable) {
Case Value:

Break
Case Value:

Break

Default
Break
}

For loop://number of known cycles

for (initialize; Boolean expression; update) {

Statement

}

Example: 9*9 law

for (int a = 1; a <= 9; a++) {
for (int b = 1; b <= A; b++) {
int c = A * b;
System.out.print (b + "*" + + "=" + c+ "");
}
System.out.println ();
}

While loop://Unknown loop count

while (Boolean expression) {

Statement

}

Example:

int a=1;
while (a<=10) {
System.out.print (A + "");
a++;
}

Break statement://Terminate current loop

Example:

int k=1;
while (k<=10) {
System.out.print (k + "");
if (k==6) {
Break
}
k++;
}

Continue statement://Exits the current loop, but does not affect the loop behind it

Example:

System.out.print ("for Loop");
System.out.println ();
for (int i = ten; i > 0; i--) {
if (i% 2 = = 0) {
Continue
}system.out.print (i+ "");
}

JAVA Getting Started programming

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.