java程式碼範例(4)

來源:互聯網
上載者:User

標籤:loop   i++   todo   ext   next   generated   span   成績   color   

 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 stub12        int i=1,sum=0;13        14        while(i<=100){15            sum=sum+i;16            i++;17        }18        System.out.println("100以內的和:"+sum);19     }20 21 }
 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 stub12        int i=1,sum=0;13        14        while(i<=100){15            sum=sum+i;16            i++;17        }18        System.out.println("100以內的和:"+sum);19     }20 21 }
 1 /** 2  * 需求分析:輸入5門成績求平均分 3  * @author chenyanlong 4  * 日期:2017/10/14 5  */ 6 package com.hp.test04; 7  8 import java.util.Scanner; 9 10 public class HS_Loop3 {11 12     public static void main(String[] args) {13         // TODO Auto-generated method stub14         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 }
 1 /** 2  * 需求分析:輸入5門成績求平均分,可以輸入多個人 3  * @author chenyanlong 4  * 日期:2017/10/14 5  */ 6 package com.hp.test04; 7  8 import java.util.Scanner; 9 10 public class HS_Loop4 {11 12     public static void main(String[] args) {13         // TODO Auto-generated method stub14         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+"門功課的成績:");26                 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 }
 1 /** 2  * 需求分析:break的用法 3  * @author chenyanlong 4  * 日期:2017/10/14 5  */ 6 package com.hp.test04; 7  8 import java.util.Scanner; 9 10 public class HS_Loop5 {11 12     public static void main(String[] args) {13         // TODO Auto-generated method stub14         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 }
 1 /** 2  * 需求分析:break的用法 3  * @author chenyanlong 4  * 日期:2017/10/14 5  */ 6 package com.hp.test04; 7  8 import java.util.Scanner; 9 10 public class HS_Loop6 {11 12     public static void main(String[] args) {13         // TODO Auto-generated method stub14         for(int i=1;i<10;i++){15             if(i==5){16                 System.out.println("迴圈提前結束");17                 break;                18             }19             System.out.println("   "+i);20         }21     }22 }

 

java程式碼範例(4)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.