The cycle of Java

Source: Internet
Author: User

Let's take an example and write a small code that implements the sum of Java code 1 to 100.

1+2+3+......+100=?

Package com.student.exam;/**
* Calculates 1 to 100 of the and
*/
public class Summation {public static void Main (string[] args) { int sum = 0;//declare variable sum to hold calculation and assign initial value to 0
The for (int i=1;i<=100;i++) sum +=i;//is equivalent to the statement sum=sum+score System.out.println ("sum=" +sum);} }

Running Result: sum=5050

The For loop is suitable for cases where the number of loops is fixed. For loops that are not fixed, such as the loop of an array and the traversal of a collection, you can use a foreach loop. The Foreach loop does not need to know the number of loops, which is explained later.

In addition, a Java implementation of 1 to 100 and

(1+100) + (2+99) + ... (50+51) =?

1 package com.student.exam; 2/** 3  * Use for loop to achieve 1 to 100 and 4  * Two add to calculate 5  * (1+100) + (2+99) + (3+98) + ... + (50+51) 6 */  7 public class
   
    summation2{8  9 public     static void Main (string[] args) {ten         int sum = 0;11         for (int i=1,j=100;i<j;i++, j--) {             sum+=i+j;13         }14         System.out.println ("sum=" +sum);     }16 17}
   

Let's implement a For loop code that calculates student scores:
Given the results of a student's 5 courses, the average output is required

 1 package com.student.exam; 2/** 3 * Use for Loop 4 * Enter the student's 5 homework, query the student's total score and average scores 5 */6 import Java.util.Scanner;    7 8 public class Averagescores {9 public static void Main (string[] args) {ten int score;    Declare a variable, named score, to receive the student's performance value one by one int sum= 0;    Declare a variable named sum, which is used to store student scores and double avg = 0.0;    Declares a variable of type double, which is used to receive the calculated pupil's average of Scanner sc = new Scanner (system.in);    Get keyboard input System.out.println ("Please enter student's name:"); String name = Sc.next ();        Assigns the obtained input to a string type name variable, i= 0; Declare a variable of type int, assign an initial value of 017 for (; i<5;i++) {//Loop 5 times Entry score of System.out.println ("Please enter 5 subjects of the" + (i+1) + "Door Score:"    ); score = Sc.nextint ();      Input results of sum + + + score; Equivalent to statement sum=sum+score; System.out.println (name+ "before" + (i+1) + "The results of the homework and is:" +sum); "System.out.println" Assembly    The performance is: "+sum"; avg = SUM/5; Calculated average score System.out.println (NAMe+ "The average score is:" +avg); 26 27}28 29} 

Because this is the basic part of Java, belongs to the initial Java, so each line of code gives a comment, for beginners more intuitive feeling, I put in the English identifier of the Chinese substitution.

Note: Although working in Eclipse works correctly in Chinese, it is recommended to use all English naming methods in real-world development.

1 package com.student.exam; 2     /** 3      * use for Loop 4      * Enter the student's 5 homework, check the student's total score and average scores 5      */6 import Java.util.Scanner; 7  8 public class asks for students '  Average result {9 public     static void Main (string[] args) {Ten         int single section score, one         int total = 0;12         Double average score =0;13         Scanner Keyboard input = new Scanner (system.in),         System.out.println ("Please enter student's name:");         String Student name = keyboard input. Next ();         = 0;17 for         (; i<5;i++) {             System.out.println ("Please enter 5 lessons of the first" + (i+1) + "Door score:");             single score = keyboard input. Nextint ();             Total Score + = Single results,             System.out.println (student name + "Pre" + (i+1) + "The results of the homework and is:" + overall score);         SYSTEM.OUT.PRINTLN (student name + "The overall score is:" + total);         Average score = Total/5;25         System.out.println (the average of student name + "is:" + average score); 26 27     }28 29}

Finally, let's use the For loop in Java to output the bitmap

 1 package com.student.exam;         2 3 public class Printtx {4 public static void main (string[] args) {5 PRINTJX (); 6 printpxsbx (); 7 PRINTSJX (); 8 9}10//print Rectangle one-by-one private static void Printjx () {i=1;i<=5;i++ for int. (int j=1;j<=5;j++) {System.out.print ("*");}16 System.out.println (); 17}18}19/ /print parallelogram private static void Printpxsbx () {(int i=1;i<=5;i++) {for (Int. k=1;k<=5-i;k + +) {System.out.print (""),}25 for (int j=1;j<=5;j++) {-Sy Stem.out.print ("*");}28 System.out.println (); 29}30}31//print triangle + private s tatic void Printsjx () {for (int i=1;i<=5;i++) {k=1;k<=5-i;k++ Stem.out.print ("");}37 for (int j=1;j<=2*i-1;j+ +) {System.out.print ("*"),}40 System.out.println (), 41}42 43 }44 45}

The results are as follows:
*****
*****
*****
*****
*****
*****
*****
*****
*****
*****

*
***
*****
*******
*********

----------------

The cycle of Java

Related Article

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.