1. 程式人生 > 其它 >【劍指offer】50:陣列中重複的數字

【劍指offer】50:陣列中重複的數字

迴圈結構

  • while迴圈

  • do...while迴圈

  • for迴圈

while迴圈

  • while是最基本的迴圈,他的結構為:
while(布林表示式){
            //迴圈內容
        }
  • 只要布林表示式為true,迴圈就會一直執行下去。
  • 我們大多數情況是會讓迴圈停下來的,我們需要讓一個表示式失效的方式來結束迴圈
  • 少部分情況需要迴圈一直執行,比如伺服器的請求響應監聽等
  • 迴圈條件一直為true就會造成無限迴圈【死迴圈】,我們正常的業務程式設計中應當避免死迴圈。會影響程式效能或者造成程式卡死崩潰!!
//練習
public class WhileDemo01 {
    
public static void main(String[] args) { //計算1+2+...+100=? int i = 0; int sum = 0; while (i<101){ sum=i+sum; i++; } System.out.println(sum); } }

do...while迴圈

  • 對於while語句而言,如果不滿足條件,則不能進入迴圈。但有時候我們需要即使不滿足條件,也至少執行一次。
  • do...while迴圈和while迴圈相似,不同的是,do...while迴圈會至少執行一次。
do{
      //程式碼語句  
}while(布林表示式);
  • while和do-while的區別:
  • while先判斷後執行。do-while先執行後判斷。
  • do-while總是保證迴圈體會被至少執行一次,這是它和while的主要差別
public class DoWhileDemo01 {
    public static void main(String[] args) {
        int a = 0;
        int i = 0;
        while (a < 0){
            i++;
            System.out.println(i);
        }
        System.out.println(
"=========="); do { i++; System.out.println(i); }while (a < 0); } }

for迴圈

  • for迴圈語句是支援迭代的一種通用結構,是最有效、最靈活的迴圈結構。
  • for迴圈執行的次數是在執行前就確定的。語法格式如下:
    for(初始化;布林表示式;更新){
           //程式碼語句     
    }
  • 練習1:計算0到100之間的奇數和偶數的和
    //練習1
    public class ForrDemo01 {
        public static void main(String[] args) {
            //練習1:計算0到100之間的奇數和偶數的和
            int sumj = 0;
            int sumo = 0;
            for (int i = 0; i <= 100; i++) {
                //奇數的和
                if (i % 2 != 0){
                    sumj = sumj + i;
                }
                if (i % 2 == 0){
                    sumo = sumo + i;
                }
            }
            System.out.println("奇數之和:"+sumj+"偶數之和:"+sumo);
    
        }
    }
  • 練習2:用while或for迴圈輸出1-1000之間能被5整除的數,並且每行輸出3個
    public class ForDemo02 {
        public static void main(String[] args) {
    //        用while或for迴圈輸出1-1000之間能被5整除的數,並且每行輸出3個
    
            //while
            int [] a = {1,2,3};
            int i = 1;
            while (i<=1000){
                if (i % 5 == 0){
    
                    System.out.print(i+"\t");
    
                }
                if (i % (5*3) == 0){
                    System.out.println("\t");
                }
    
                i++;
            }
    
        }
    }
    
    
    
    public class ForDemo03 {
        public static void main(String[] args) {
            //for
            for (int i = 0; i <= 1000; i++) {
    
                if (i % 5 == 0){
                    System.out.print(i+"\t");
                }
                if (i % 15 == 0){
                    System.out.println();
                }
    
            }
        }
    }
  • 練習3:列印九九乘法表
    public class ForDemo04 {
        public static void main(String[] args) {
    //        練習3:列印九九乘法表
            for (int i = 1; i < 10; i++) {
                for (int a = 1; a <= i; a++){
                    System.out.print(a + "*" + i + "=" + (i*a)+"\t");
                }
                System.out.println();
    
            }
        }
    }

關於for迴圈有以下幾點說明:
最先執行初始化步驟。可以宣告一種型別, 但可初始化-個或多個就環控制變數,也可以是空語句。
然後,檢測布林表示式的值。如果為true,迴圈體被執行。如果為false,迴圈終止,開始執行迴圈體後面的語旬。
執行一次迴圈後,更新迴圈控制變數(迭代因子控制迴圈變數的增減)。
再次檢測布林表示式。迴圈執行上面的過程。