<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                >[success] # while -- 循環 * 循環條件成立進入循環體,循環條件不成了就跳出循環 ~~~ while(條件表達式) { 循環體; } ~~~ >[danger] ##### 計算累加 ~~~ public class SumTest { public static void main(String[] args) { int i = 1; int sum = 0; while (i <= 100) { sum += i; i++; } System.out.print(sum); } } ~~~ >[danger] ##### 輸入數取反 * `123`為例子 先獲取個位即`123 % 10` 得到`3` 將 `十位2 變成個位` 即 `123 / 10` 兩個int 相除得到是 int 類型 得到了 `12` 在繼續上一步 `12 % 10` 得到 `2`,依次 類推直到 `1 / 10` 兩個 int 類型 相除 得到 `0` 即每一位都進行了相除循環結束 ~~~ import java.util.Scanner; class WhileReverse { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); while (n > 0) { int i = n % 10; // 取余數 獲取當前個位數 System.out.print(i); n = n / 10; // 取整數 獲取剔除最后一位 } } } ~~~ * 循環并記錄 ,每循環一次相對擴大10倍 ~~~ import java.util.Scanner; class WhileReverse { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int res = 0; while (n > 0) { res = res * 10 + n % 10; // 3 32 321 n /= 10; // 12 1 0 } System.out.println(n + "逆序后的結果是:" + res); } } ~~~ >[danger] ##### 求余數案例 ~~~ public class Test { public static void main(String[] args) { /* * 需求:給定兩個整數,被除數和除數(都是正數,且不超過int的范圍) 。 * 將兩數相除,要求不使用乘法、除法和 % 運算符。 * 得到商和余數。 * * 分析: * 被除數 / 除數 = 商 ... 余數 * * int a = 100; * int b = 10; * * 100 - 10 = 90 * 90 - 10 = 80 * 80 - 10 = 70 * 70 - 10 = 60 * ... * 10 - 10 = 0 (余數) * */ // 1.定義變量記錄被除數 int dividend = 100; // 2.定義變量記錄除數 int divisor = 37; // 3.定義一個變量用來統計相減了多少次 int count = 0; // 3.循環 while // 在循環中,不斷的用被除數 - 除數 // 只要被除數 是大于等于除數的,那么就一直循環 while (dividend >= divisor) { dividend = dividend - divisor; // 只要減一次,那么統計變量就自增一次 count++; } // 當循環結束之后dividend變量記錄的就是余數 System.out.println("余數為:" + dividend); // 當循環結束之后,count記錄的值就是商 System.out.println("商為:" + count); } } ~~~ >[danger] ##### 回文數 ~~~ public class Test { public static void main(String[] args) { /*需求:給你一個整數 x 。 如果 x 是一個回文整數,打印 true ,否則,返回 false 。 解釋:回文數是指正序(從左向右)和倒序(從右向左)讀都是一樣的整數。 例如,121 是回文,而 123 不是。*/ //分析: //1.定義變量記錄整數 int x = 12345; //把x做一個臨時存儲,用來最后進行判斷 int temp = x; //2.定義變量記錄最終的結果(反過來的數字) int result = 0; //3.利用循環從右往左獲取x中的數字并拼接到result當中 //while while(x != 0){ //獲取到x最右邊的數字 int ge = x % 10; //獲取一次數字之后,那么就要把當前最右邊的數字給去掉 x = x / 10; //拼接到result當中 result = result * 10 + ge; } System.out.println(result == temp); } } ~~~ >[info] ## while 和 for 不同點 * while循環更適合于明確循環條件但不明確循環次數的場合中。 * or循環更適合于明確循環次數或范圍的場合中。 * while(true) 等價于 for(;;) 都表示無限循環
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看