1 /*
2 需求分析:计算100以内的和,用while
3 @author chenyanlong
4 日期:2017/10/14
5 */
6 package com.hp.test04;
7
8 public class HS_Loop1 {
9
10 public static void main(String[] args) {
11 // TODO Auto-generated method stub
12 int i=1,sum=0;
//代码效果参考:http://0791zd.com/zx/art_7066.html
13
14 while(i<=100){
15 sum=sum+i;
16 i++;
17 }
18 System.out.println("100以内的和:"+sum);
19 }
20
21 }
复制代码
1 /*
2 需求分析:输入5门成绩求平均分
8 import java.util.Scanner;
10 public class HS_Loop3 {
11
12 public static void main(String[] args) {
13 // TODO Auto-generated method stub
14 String name;
15 double sum=0,score;
16 double avg=0;
17 System.out.println("请输入姓名:");
18
19 Scanner input= new Scanner(System.in);
20 name=input.next();
21 //System.out.println(name);
22 for(int i=1;i<=5;i++){
23 System.out.print("请输入5门功课的第"+i+"门功课的成绩:");
24
25 Scanner input2=new Scanner(System.in);
26 score=input2.nextDouble();
27 sum=sum+score;
28 }
29 avg=sum/5;
30 System.out.println(name+"的平均分是:"+avg);
31 }
32 }
2 * 需求分析:输入5门成绩求平均分,可以输入多个人
10 public class HS_Loop4 {
14 String letter=null;
15 do{
16 String name;
17 double sum=0,score;
18 double avg=0;
19 System.out.println("请输入姓名:");
20
21 Scanner input= new Scanner(System.in);
22 name=input.next();
23 //System.out.println(name);
24 for(int i=1;i<=5;i++){
25 System.out.print("请输入5门功课的第"+i+"门功课的成绩:");
//代码效果参考:http://0791zd.com/zx/art_5562.html
27 Scanner input2=new Scanner(System.in);
28 score=input2.nextDouble();
29 sum=sum+score;
30 }
31 avg=sum/5;
32 System.out.println(name+"的平均分是:"+avg);
33
34 System.out.print("继续输出吗?(Y/N):");
35 Scanner input3=new Scanner(System.in);
36 letter=input3.next();
37 }while(letter.equals("Y")||letter.equals("y"));
38 System.out.println("录入完毕");
39 }
40 }
2 * 需求分析:break的用法
10 public class HS_Loop5 {
14 for(int i=1;i<10;i++){
15 if(i==5){
16 System.out.println("跳出一次");
17 continue;
18 }
19 System.out.println(" "+i);
20 }
21 }
22 }
10 public class HS_Loop6 {
16 System.out.println("循环提前结束");
17 break;