day04-java-迴圈結構(while、do-while、for)

來源:互聯網
上載者:User

標籤:運算   執行   加法   ring   實現   round   過程   互動   while迴圈   

    迴圈結構(while、do-while、for)    

任何複雜的程式邏輯都可以通過三種結構來實現:
1)順序結構:從上往下逐行執行,每句必走
2)分支結構:有條件的執行某語句一次,並非每句必走
3)迴圈結構:有條件的執行某語句多次,並非每句必走


1.迴圈:反覆執行一段相同或相似的代碼


2.迴圈三要素:
  1)迴圈變數的初始化
  2)迴圈的條件(以迴圈變數為基礎)
  3)迴圈變數的改變(向著迴圈的結束變)
  迴圈變數:在迴圈過程中所改變的那個量


3.迴圈結構:
  1)while:先判斷後執行,有可能一次都不執行
  2)do...while:先執行後判斷,至少執行一次
               第1要素與第3要素相同時首選
  3)for:應用率最高,固定次數迴圈


4.break:跳出迴圈
  continue:跳過迴圈體中剩餘語句而進入下一次迴圈

Math.random():
Math.random()-------------0.0到0.9999999999...
*1000---------------------0.0到999.99999999...
+1------------------------1.0到1000.9999999...
(int)---------------------0到999
+1------------------------1到1000

for-隨機加法運算器
package day04;import java.util.Scanner;//隨機加法運算器public class Addition {    public static void main(String[] args) {        Scanner scan = new Scanner(System.in);        int score = 0; //總分        for(int i=1;i<=10;i++){ //10次            int a = (int)(Math.random()*100); //加數1            int b = (int)(Math.random()*100); //加數2            int result = a+b; //和            System.out.println("("+i+")"+a+"+"+b+"=?"); //1.出題            System.out.println("算吧!");            int answer = scan.nextInt(); //2.答題            if(answer==-1){ //3.判題                System.out.println("下次再來吧!");                break;             }            if(answer==result){                score += 10; //答對一題加10分                System.out.println("答對了");            }else{                System.out.println("答錯了");            }        }        System.out.println("score="+score);    }}
while迴圈示範:
package day04;//while迴圈的示範public class Addition {    public static void main(String[] args) {        int num = 1;        while(num<10){            System.out.println(num+"*9="+num*9);            num++;        }        System.out.println("over");    }}
do while迴圈示範:
package day04;import java.util.Scanner;//猜數字小遊戲public class Addition {    public static void main(String[] args) {        Scanner scan = new Scanner(System.in);        int num = (int)(Math.random()*1000+1); //1到1000之內的隨機數        System.out.println(num); //作弊        int guess;        do{            System.out.println("猜吧!");            guess = scan.nextInt(); //1,3            if(guess==0){                break;            }            if(guess>num){                System.out.println("太大了");            }else if(guess<num){                System.out.println("太小了");            }        }while(guess!=num); //2        if(guess==num){            System.out.println("恭喜你,猜對了!");        }else{            System.out.println("下次再來吧!");        }    }}        
 數列求和
計算1+2+3+4+5...+100的和;
package day04;//計算1+2+3+4+5...+100的和;import java.util.Scanner;public class Addition {    public static void main(String[] args) {        int num = 0;        for(int i=1;i<=100;i++){            num = num + i;        }        System.out.println(num);    }}
結果:5050

有數列為:9,99,999,...,9999999999。要求使用程式計算此數列的和,並在控制台輸出結果:

    public class SumOfSeq {        public static void main(String[] args) {            // 數列求和            long nine = 9;            long result = nine;            for (int i = 2; i <= 10; i++) {                nine = nine * 10 + 9;                result += nine;            }            System.out.println("9+99+999+...+9999999999=" + result);        }    }

另有數列:1+1/2+1/3…+1/n(n>=2)。要求使用互動的方式計算此數列的和:使用者在控制台錄入需要計算的整數 n 的值,程式計算此數列的和,並在控制台輸出結果。

    import java.util.Scanner;    public class SumOfSeq2 {        public static void main(String[] args) {            Scanner scanner = new Scanner(System.in);            System.out.println("請輸入整數(例如10):");            int n = scanner.nextInt();            double result = 0;            for (int i = 1; i < n; i++) {                result += 1.0 / i;                if(i==1){                    System.out.print("1+");                }else{                    System.out.print("1/" + i + "+");                    }            }            result += 1.0 / n;            System.out.print("1/" + n + "=" + result);        }    }

 

day04-java-迴圈結構(while、do-while、for)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.