Basic Java syntax and java syntax

Source: Internet
Author: User

Basic Java syntax and java syntax
Loop

Use a for loop to calculate the value of 1 + 3 + 5 + 7 +... + 99 and output the calculation result.

Calculate 1 with a for loop! + 2! + 3! +... + 10! And output the calculation result.

Output 1 ~ The first five in 100 can be divided by three.

Output 101 ~ Prime number within 200.

// Use a for loop to calculate the value of 1 + 3 + 5 + 7 +... + 99 and output the calculation result. Public class Test {/*** @ param args */public static void main (String [] args) {// TODO Auto-generated method stub int add = 0; for (int I = 1; I <= 99; I + = 2) {add + = I;} System. out. println ("1 + 3 + 5 + 7 +... + 99 = "+ add );}}
Printed result:
1+3+5+7+...+99 = 2500

1 // calculate 1 with a for loop! + 2! + 3! +... + 10! And output the calculation result. 2 public class Test {3/** 4 * @ param args 5 */6 public static void main (String [] args) {7 // TODO Auto-generated method stub 8 long result = 0; 9 long f = 1; 10 for (int I = 1; I <= 10; I ++) {11 f = f * I; 12 result + = f; 13 14} 15 System. out. println ("1! + 2! + 3! +... + 10! = "+ Result); 16} 17}

Printed result:
1! + 2! + 3! +... + 10! = 4037913
1 // output 1 ~ The first five in 100 can be divided by three. 2 public class Test2 {3 4/** 5 * @ param args 6 */7 public static void main (String [] args) {8 // TODO Auto-generated method stub 9 int num = 0, I = 1; 10 while (I <= 100) {11 if (I % 3 = 0) {12 System. out. println (I + ""); 13 num ++; 14} 15 if (num = 5) 16 break; 17 I ++; 18} 19 20} 21 22}
Print result:

3
6
9
12
15

1 // outputs 101 ~ Prime number within 200. 2 public class Test2 {3 4/** 5 * @ param args 6 */7 public static void main (String [] args) {8 // TODO Auto-generated method stub 9 for (int I = 101; I <200; I ++ = 2) {10 boolean f = true; 11 for (int j = 2; j <I; j ++) {12 if (I % j = 0) {13 f = false; 14 break; 15} 16} 17 if (! F) 18 continue; 19 System. out. print ("" + I); 20} 21} 22}

 


Basic Java syntax

What's more? The basic syntax is similar to the C language, except that a basic String data type is added to the basic syntax without a pointer.
 
Basic JAVA syntax

This is not a matter of books. You just need to repeat your own code. You have the foundation of C # and learn it quickly.
I really want to find a book. Let's look at the JDK help documentation. In fact, it's not messy. It's hard to learn it by myself.

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.