1. 程式人生 > 實用技巧 >面試題17_2:實現兩個大數相加(包含負數)

面試題17_2:實現兩個大數相加(包含負數)

假設,要你在控制檯輸出1到100,這個時候怎麼輸出,emmmmm我可以寫100個輸出語句。幹得漂亮!

for迴圈:

for迴圈的結構:

for迴圈練習:

01:列印1到100的累加和:

public static void main(String[] args) {
         /* 列印1到100的累加和 */
        int num = 0 ;  //初始化num變數來算累加和
        for (int i = 1; i <=100; i++) {   //開始迴圈從1開始到100
            num = num + i;          //每迴圈一次都把i與num相加一遍
        }
        System.out.println(num);    
//輸出累加和num 結果為5050。 }

01:列印九九乘法表:

public static void main(String[] args) {
        /* 
         * 列印九九乘法表 例如1 x 1 = 1 
         * 我把前面的1看做是i, 把後面的看做是j
         * */
        for (int i = 1; i <= 9; i++) {    //定義i迴圈九次
            for (int j = 1; j <= i; j++) { //定義j內部迴圈的次數等於 i
                System.out.print(i+"x"+j+"="+i*j+"\t");  //
一次打印出乘法口訣表 } System.out.println("\n");//當內部迴圈執行完時,進行換行 } }

for迴圈的變換:

    public static void main(String[] args) {
        /* for迴圈的變換以九九乘法表為例 */
        
        int i =1;  //將變數i放在對應for迴圈外
        for (; i <= 9;) {
            int j = 1;//將變數j放在對應for迴圈外
            for
(; j <= i;) { System.out.print(i+"x"+j+"="+i*j+"\t"); j++; //將j++放在對應的for迴圈的方法體內 } i++; //將i++放在對應的for迴圈的方法體內 System.out.println("\n");//當內部迴圈執行完時,進行換行 } /*注:無論放在括號裡面還是放在外面,for迴圈括號中的分號不能少必須是兩個,且是英文*/ }

while迴圈:

while迴圈可以說和for迴圈如出一轍沒有太大的區別

while迴圈結構:

public static void main(String[] args) {
        int a =1;      //宣告變量表達式a 並賦初始值為1
        while (a<2) {    //布林表示式a是否小於2  如果為true則執行方法體 ,如果為flase則跳出迴圈
            System.out.println(a);   //方法體
        }
    }

while迴圈練習:

01.列印1到100內所有偶數:

public static void main(String[] args) {
        /* 使用while迴圈輸出1到100以內所有的偶數; */
        int i = 1;     //初始化i
        while (i < 100) {     //表示式小於100
            if (i % 2 == 0) {    //判斷i對2取餘是否為0
                System.out.println(i);   //輸出i
            }
            i++;     //i進行加加
        }
    }

do...while迴圈:

do...while結構:

    public static void main(String[] args) {
        int x =1;     //宣告變量表達式
        do {
            System.out.println(x);    //括號裡面是迴圈體
            x++;
        } while (x<10);   //布林表示式
    }

do...while特點:

do...while迴圈的特點:無條件執行一次迴圈體,即使我們將迴圈條件直接寫成false,也依然會迴圈一次。

三大迴圈之間的區別:

  • 首先結構上的不同,就是寫法不一樣;
  • for迴圈可以變換著寫,while,do...while不可以
  • for迴圈和while迴圈先判斷布林表示式是否成立在執行方法體,do...while先執行方法體再執行布林表示式
  • for迴圈運用在迴圈次數已知的情況下,while,do...while運用在迴圈次數未知的情況下
  • 建議使用的順序:for,while,do-while

三大迴圈對應的死迴圈:

  • for: for( ; ; ){}
  • while: while(true){}
  • do...while: do{ }while(true);

例子如下:

public static void main(String[] args) {
        //  三大死迴圈;
         for (;;) { System.out.println("我是for死迴圈"); }
         

         while (true) { System.out.println("我是while死迴圈"); }
         

        do {
            System.out.println("我是do...while死迴圈");
        } while (true);
    }

注:這三給死迴圈不能同時出現,一次只能出現一種。

Random隨機數:

Random是java API提供是一個類,它可以向我們提供一個隨機數。

語法格式:

package com.code_test01;

import java.util.Random;   //導包

public class Dome08 {
    public static void main(String[] args) {
        Random random = new Random();//例項化Random類
          /*呼叫nextInt方法,預設為int類型範圍之間的隨機數*/
        
          int nextInt = random.nextInt();//呼叫random類裡面的nextInt方法
          System.out.println(nextInt);   //輸出nextInt
          
          int nextInt2 = random.nextInt(100);  //隨機產生一個0到99的隨機整數,包含零不包含100
          System.out.println(nextInt2);
    }
}

隨機數沒有深入的去了解,好像其他的有些資料型別不能正常方法的使用。

綜合練習01:

public static void main(String[] args) {
        /*
         * 猜拳遊戲 規則 :你和電腦猜拳,1代表石頭,2代表剪刀 ,3代表布 電腦隨機出1~3,你鍵盤錄入,
         */
        Random random = new Random();
        Scanner sc = new Scanner(System.in);
        int randInt = random.nextInt(3) + 1;
        System.out.println(randInt);
        System.out.println("電腦已就位,請出拳");
        int nextInt2 = sc.nextInt();
        //方式1
        if (randInt == nextInt2) {
            System.out.println("你們平局");
        } else if (randInt == 1 & nextInt2 == 2 || randInt == 2 & nextInt2 == 3 || randInt == 3 & nextInt2 == 1) {
            System.out.println("抱歉,你輸給了電腦,真沒用");
        } else if (randInt == 2 & nextInt2 == 1 || randInt == 3 & nextInt2 == 2 || randInt == 1 & nextInt2 == 3) {
            System.out.println("恭喜你,你贏了電腦,真棒");
        } else {
            System.out.println("叫你輸入1 2 3,輸錯了懂不懂?重新輸去");
            main(args);
        }
        //方式2
        if(nextInt2 - randInt ==0) {
            System.out.println("你們為平局");
        }else if (nextInt2 - randInt == 1) {
            System.out.println("抱歉,你輸給了電腦,真沒用");
        }else if (nextInt2 - randInt == -1) {
            System.out.println("恭喜你,你贏了電腦,真棒");
        }else if (nextInt2 - randInt == -2) {
            System.out.println("抱歉,你輸給了電腦,真沒用");
        }else {
            System.out.println("叫你輸入1 2 3,輸錯了懂不懂?重新輸去");
            main(args);
        }
    }
//這有兩個方式,記得註釋掉一個

綜合案例02:

public static void main(String[] args) {
        /*
         * 利用隨機數生成1~100的數,使用者來猜大小
         */
        Random random = new Random();
        int ranInt = random.nextInt(100)+1;
        try (Scanner sc = new Scanner(System.in)) {
            while (true) {
                System.out.println("請輸入你猜的數:");
                int nextInt = sc.nextInt();
                if (nextInt > ranInt) {
                    System.out.println("不好意思,你猜的數大了,請重新猜一下哦");
                }
                if (nextInt < ranInt) {
                    System.out.println("不好意思,你猜的數小了,請重新猜一下哦");
                }
                if (nextInt == ranInt) {
                    System.out.println("恭喜你,猜的剛剛好");
                    break;
                }
            }
        }
    }

綜合案例03:

public static void main(String[] args) {
        //練習一: 列印金字塔
         int tem = 10;
            for (int i = 1; i <= tem; i++) {
                for (int j2 = 1; j2 <= tem - i; j2++) {
                    System.out.print(" ");
                }
                for (int j = 1; j <= i * 2 - 1; j++) {
                    System.out.print("*");
                }
                System.out.println();
            }
        //練習2  個數如果恰好等於它的因子之和,這個數就稱為 "完數 "。例如6=1+2+3,找出1000以內的所有完數。
            for (int i = 1; i < 1000; i++) {
                int temp = 0;
                for (int j = 1; j < i; j++) {
                    if (i % j == 0) {
                        temp = temp + j;
                    }
                }
                if (temp == i) {
                    System.out.println(i);
                }
            }
    }

個人學習,內容簡略。