2013年7月28日星期日

Java Beginner _java cycle _for loop in simple example

 

Java based portal - loop for circulating a simple example

 

first look for the grammatical structure:

 
  
for(表达式1;表达式2;表达式3){ 
循环语句
}
 
 

or more intuitive representation of this:

 
  
for(变量初始化;循环条件;迭代语句){ 
循环语句
}
 
 

for loop execution processes are:

 

performed first expression 1, is generally carried variable initialization and then execute the expression 2, namely loop condition to judge, if the result is true, the loop body is executed; circulation After executing body, the Executive expression 3, changing the value of the loop variable, again executing the expression 2; evaluates to true, the loop continues; if the result is false, the loop is terminated, execute the statement following.

 No matter how many times

execution, the expression an executed only once.

 

we give an example, write a little code to achieve the java code 1-100 summation?

 

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

 
  
package com.student.exam; 

/**
*计算1到100的和
*/
public class Summation {

public static void main(String[] args) {
int sum = 0; //声明变量sum存放计算的和,赋初值为0
for(int i=1;i<=100;i++)
sum
+=i;//等价于语句sum=sum+score
System.out.println("sum="+sum);
}

}
 
 

Run Results: sum = 5050

 

for loop suitable for a fixed number of cycles the situation. The number of cycles is not fixed, such as arrays and collections cycle traversal, you can use a foreach loop. foreach loop does not need to know the number of cycles, the later explanation.

 

it another adder using java 1-100 and

 

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

 
  
 1 package com.student.exam; 
2 /**
3 * 使用for循环实现1到100的和
4 * 两头相加进行计算
5 * (1+100)+(2+99)+(3+98)+... ...+ (50+51)
6 */
7 public class Summation2{
8
9 public static void main(String[] args) {
10 int sum = 0;
11 for(int i=1,j=100;i<j;i++,j--){
12 sum+=i+j;
13 }
14 System.out.println("sum= "+sum);
15 }
16
17 }
 
 

Let us realize that the calculation of student achievement for loop code:
assume given a student's homework grades 5, the required output averages

 
  
 1 package com.student.exam; 
2 /**
3 * 使用for 循环
4 * 输入学生的5们功课,查询该学生的总成绩和平均分数
5 */
6 import java.util.Scanner;
7
8 public class AverageScores {
9 public static void main(String[] args) {
10 int score; //声明一个变量,取名为score,用来接收学生的成绩值
11 int sum= 0; //声明一个变量,取名sum,用来存储学生的成绩和
12 double avg =0.0; //声明一个double类型的变量,用来接收计算出来的学生的平均分
13 Scanner sc = new Scanner(System.in); //获取键盘输入
14 System.out.println("请输入学生的姓名:");
15 String name = sc.next(); //将获取的输入赋值给一个String类型的name变量
16 int i= 0; //声明一个int类型的变量,赋初始值为0
17 for(;i<5;i++){ //循环5次录入成绩
18 System.out.println("请输入5门功课的第"+(i+1)+"门的成绩:");
19 score = sc.nextInt(); //录入成绩
20 sum += score; //等价于语句 sum=sum+score;
21 System.out.println(name+"的前"+(i+1)+"门功课的成绩和是:"+sum);
22 }
23 System.out.println(name+"的总成绩是:"+sum);
24 avg = sum/5; //计算平均成绩
25 System.out.println(name+"的平均分是:"+avg);
26
27 }
28
29 }
 
 

because this is a fundamental part of Java, are the initial java, so every line of code are given notes, more intuitive feel for beginners, I put one of the English character identifier for to do instead.

 

Note: Although the use of Chinese in Eclipse to run, but in the actual development, it is recommended to use the full English are named.

 
  
 1 package com.student.exam; 
2 /**
3 * 使用for 循环
4 * 输入学生的5们功课,查询该学生的总成绩和平均分数
5 */
6 import java.util.Scanner;
7
8 public class 求学生的平均成绩 {
9 public static void main(String[] args) {
10 int 单科成绩;
11 int 总成绩= 0;
12 double 平均分 =0;
13 Scanner 键盘输入 = new Scanner(System.in);
14 System.out.println("请输入学生的姓名:");
15 String 学生姓名 = 键盘输入.next();
16 int i= 0;
17 for(;i<5;i++){
18 System.out.println("请输入5门功课的第"+(i+1)+"门的成绩:");
19 单科成绩 = 键盘输入.nextInt();
20 总成绩 += 单科成绩;
21 System.out.println(学生姓名+"的前"+(i+1)+"门功课的成绩和是:"+总成绩);
22 }
23 System.out.println(学生姓名+"的总成绩是:"+总成绩);
24 平均分 = 总成绩/5;
25 System.out.println(学生姓名+"的平均分是:"+平均分);
26
27 }
28
29 }
 
 

Finally, we use the java for loop output raster graphics

 
  
 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 //打印矩形
11 private static void printJX(){
12 for(int i=1;i<=5;i++){
13 for(int j=1;j<=5;j++){
14 System.out.print("*");
15 }
16 System.out.println();
17 }
18 }
19 //打印平行四边形
20 private static void printPXSBX(){
21 for(int i=1;i<=5;i++){
22 for(int k=1;k<=5-i;k++){
23 System.out.print(" ");
24 }
25 for(int j=1;j<=5;j++){
26 System.out.print("*");
27 }
28 System.out.println();
29 }
30 }
31 //打印三角形
32 private static void printSJX() {
33 for(int i=1;i<=5;i++){
34 for(int k=1;k<=5-i;k++){
35 System.out.print(" ");
36 }
37 for(int j=1;j<=2*i-1;j++){
38 System.out.print("*");
39 }
40 System.out.println();
41 }
42
43 }
44
45 }
 
 

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

 

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

 

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

 

 

 

 

 

 

 

 

 

1 条评论:

  1. Thank you, I'm still somewhat confused though have an outline of what you mean

    回复删除