Java學(xué)習(xí)筆記 - 第006天

每日要點

方法的重載

方法的重載: 在一個類中可以出現(xiàn)同名方法 只要它們的參數(shù)列表不同就能夠加以區(qū)分闰靴。
參數(shù)列表不同指的是: 參數(shù)的類型不相同或者參數(shù)的個數(shù)不相同或者二者皆不同哲思。

    public static double perimeter(double width, double height) {
        return (width + height) * 2;
    }
    
    public static double area(double width, double height) {
        return width * height;
    }
    
    public static double perimeter(double a, double b, double c) {
        return a + b + c;
    }
    
    public static double area(double a, double b, double c) {
        double half = perimeter(a, b, c) /2;
        return Math.sqrt(half * (half - a) * (half - b) * (half - c));
    }

引用其他類的方法

靜態(tài)導(dǎo)入(不常用的語法)

import static com.jack.Test02.*;

類名.方法

double fencePrice = Test02.perimeter(r + 3) * FENCE_UNIT_PRICE;

數(shù)組

數(shù)組 - 用一個變量保存多個同種類型的值
可以用以下來創(chuàng)建數(shù)組
int[] f = { 0, 0, 0, 0, 0, 0};
int[] f = new int[6];
數(shù)組的length屬性
f.length 表示這個數(shù)組的長度
for-each循環(huán) (Java 5+)
給數(shù)組每一個找一個代言人央勒,如循環(huán)開始 int i 來 代替 f[0]寺滚,循環(huán)一次 代替下一個數(shù)
只讀循環(huán)

        for (int i : f) {
            System.out.println(i);
        }

排序

  • 冒泡排序: 每2個進行比較斑鼻,大的往后走喻圃,小的往前走萤彩。循環(huán)一次找到一個最大
public class Test06 {

    public static void main(String[] args) {
        int[] x = { 23, 67, 12, 99, 58, 77, 88, 4, 45, 81};
        bubbleSort(x);
        for (int a : x) {
            System.out.print(a + " ");
        }
    }

    public static void bubbleSort(int[] array) {
        boolean swapped = true;     
        // 冒泡排序 
        for (int i = 1; swapped && i <= array.length - 1; i++) {
            swapped = false;
            for (int j = 0; j < array.length - i; j++) {
                if (array[j] > array[j + 1]) {
                    // 交換兩個元素
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                    swapped = true;
                }
            }
        }
    }
  • 選擇排序:第一個與后面所有數(shù)比較玖姑,選出最小的放在第一位

周末作業(yè)講解

  • 1. 5個人去打魚裹驰,打了很多魚
    a、b荒叶、c肆汹、d愚墓、ea 先醒過來,分5份昂勉,多了一條浪册,扔掉,拿走自己的一份
    b 然后醒過來岗照,分5份村象,多了一條笆环,扔掉,拿走自己的一份
    ...
    最少要捕多少魚
        for (int i = 1; ; i++) {
            int total = i;
            boolean isEnough = true;
            for (int j = 1; j <= 5; j++) {
                if ((total - 1) % 5 == 0) {
                    total = (total - 1) / 5 * 4;
                }
                else {
                    isEnough = false;
                    break;
                }
            }
            if (isEnough) {
                System.out.println(i);
                break;
            }
        }

練習(xí)

  • 1.求過道和圍墻花費多少錢
練習(xí)1.png

初始:

        final double PI = 3.14;
        Scanner input = new Scanner(System.in);
        System.out.print("請輸入游泳池的半徑(m): ");
        double r1 = input.nextDouble();
        double r2 = r1 + 3;
        
        double area = r2 * r2 * PI - r1 * r1 * PI;
        double perimeter = 2 * r2 * PI;
        
        double total = area * 38.5 + 15.5 * perimeter;
        System.out.printf("圍墻花費%.2f元,過道花費%.2f元,總共花費%.2f元.", 
                perimeter * 15.5, area * 38.5, total);
        input.close();

修改:

        Scanner input = new Scanner(System.in);
        System.out.print("請輸入游泳池的半徑: ");
        double r = input.nextDouble();
        if (r > 0) {
            double fencePrice = perimeter(r + 3) * FENCE_UNIT_PRICE;
            double aislePrice = (area(r + 3) -
                    area(r)) * AISLE_UNIT_PRICE;
            System.out.printf("圍墻的造價為: %.2f元\n", fencePrice);
            System.out.printf("過道的造價: %.2f元", aislePrice);
        }
        else {
            System.out.println("游泳池的半徑應(yīng)該是一個正數(shù).");
        }
        input.close();
    }

    public static double perimeter(double radius) {
        return 2 * Math.PI * radius;
    }
    
    public static double area(double radius) {
        return Math.PI * radius * radius;
    }
  • 2. 排列數(shù): A(m,n) = m! / n!
    計算組合數(shù): C(m,n) = m! / n! / (m -n)!

    首先:
        Scanner input = new Scanner(System.in);
        System.out.print("m = ");
        int m = input.nextInt();
        System.out.println("n = ");
        int n = input.nextInt();
        if (m >= n) {   
            double result = 1;
            for (int i = 2; i <= m; i++) {
                result *= i;
            }
            double fm = result;
            result = 1;
            for (int j = 2; j <= n; j++) {
                result *= j;
            }
            double fn = result;
            result = 1;
            for (int k = 2; k <= m - n; k++) {
                result *= k;
            }
            double fmn =result;
            
            System.out.printf("C(%d,%d) = %.0f\n",
                    m, n, fm / fn / fmn);
        }
        else {
            System.out.println("輸入錯誤.");
        }
        input.close();

修改:

        Scanner input = new Scanner(System.in);
        System.out.print("m = ");
        int m = input.nextInt();
        System.out.println("n = ");
        int n = input.nextInt();
        if (m >= n) {   
            System.out.printf("C(%d,%d) = %.0f\n",
                    m, n, f(m) / f(n) / f(m - n));
        }
        else {
            System.err.println("輸入錯誤.");
        }
        input.close();
    }
    public static double f(int n) {
        double fn =1;  // 保存n的階乘的變量
        for (int i = 2; i <= n; i++) {
            fn *= i;
        }
        return fn;
    }
  • 3.有30個人(15個基督教徒和15個非教徒)坐船 船壞 要把15個人扔到海里其他人才能得救
    圍成一圈從某個人開始從1報數(shù) 報到9的人扔到海里 下一個人繼續(xù)從1開始報數(shù) 報到9扔到海里
    以此類推 直到把15個人扔到海里為止 結(jié)果由于上帝的保佑15個基督教徒都幸免于難
    問這些人最初是怎么站的 哪些位置是基督徒 哪些位置是非教徒 --- Josephu環(huán)(約瑟夫環(huán))
        boolean[] persons = new boolean[30];
        for (int i = 0; i < persons.length; i++) {
            persons[i] = true;
        }
        
        int counter = 0;
        int number = 0;
        int index = 0;
        while(counter < 15 ) {
            if (persons[index]) {
                number += 1;
                if (number == 9) {
                    persons[index] = false;
                    counter += 1;
                    number = 0;
                }
            }
            index += 1;
            if (index == 30) {
                index = 0;
            }
        }
        for (boolean b : persons) {
            System.out.print(b ? "基" : "非");
        }

例子

  • 1.使用方法厚者,求三角形和矩形的周長和面積
        Scanner input = new Scanner(System.in);
        System.out.print("請輸入三角形三條邊的長度: ");
        double a = input.nextDouble();
        double b = input.nextDouble();
        double c = input.nextDouble();
        if (isValidForTriangle(a, b, c)) {
            System.out.println("周長: " + perimeter(a, b, c));
            System.out.println("面積: " + area(a, b, c));
        }
        else {
            System.err.println("不能構(gòu)成三角形");
        }
        System.out.print("請輸入矩形的寬和高: ");
        double width = input.nextDouble();
        double height = input.nextDouble();
        System.out.println("周長: " + perimeter(width, height));
        System.out.println("面積: " + area(width, height));
        input.close();
    }
    
    public static double perimeter(double radius) {
        return 2 * Math.PI * radius;
    }
    
    public static double area(double radius) {
        return Math.PI * radius * radius;
    }
    // 方法的重載: 在一個類中可以出現(xiàn)同名方法 只要它們的參數(shù)列表不同就能夠加以區(qū)分
    // 參數(shù)列表不同指的是: 參數(shù)的類型不相同或者參數(shù)的個數(shù)不相同或者二者皆不同
    public static double perimeter(double width, double height) {
        return (width + height) * 2;
    }
    
    public static double area(double width, double height) {
        return width * height;
    }
    
    public static double perimeter(double a, double b, double c) {
        return a + b + c;
    }
    
    public static double area(double a, double b, double c) {
        double half = perimeter(a, b, c) /2;
        return Math.sqrt(half * (half - a) * (half - b) * (half - c));
    }
    
    public static boolean isValidForTriangle(double a, double b, double c) {
        return a + b > c && a + c > b && b + c > a; 
    }
  • 2.數(shù)組:搖骰子
        int[] f = new int[6];
        for (int i = 1; i <= 60000; i++) {
            int face = (int) (Math.random() * 6 + 1);
            f[face - 1] += 1;
        }
        for (int i = 1; i <= 6; i++) {
            System.out.println(i + "點搖出了" + f[i - 1] + "次");
        }
  • **3.斐波那契 數(shù)列 **
        int[] f = new int[20];
        f[0] = f[1] = 1;
        // f.lenth 屬性 
        for (int i = 2; i < f.length; i++) {
            f[i] = f[i - 1] + f[i - 2];
        }
        /*for (int i = 0; i < f.length; i++) {
            System.out.println(f[i]);
        }*/
        
        // for-each循環(huán) (Java 5+)
        // 給數(shù)組每一個找一個代言人  如循環(huán)開始 int i 來 代替 f[0] 循環(huán)一次 代替下一個 數(shù)
        // 只讀循環(huán) 
        for (int i : f) {
            System.out.println(i);
        }
  • 4.用一個數(shù)組來存學(xué)生的成績躁劣,算最高和最低分,平均分
        String[] names = {"關(guān)羽", "張飛", "黃忠", "趙云", "馬超"};
        double[] scores = new double[names.length];
        Scanner input = new Scanner(System.in);
        for (int i = 0; i < scores.length; i++) {
            System.out.print("請輸入" + names[i] + "的成績: ");
            scores[i] = input.nextDouble();
        }
        input.close();
        double sum = 0;
        for (int i = 0; i < scores.length; i++) {
            sum += scores[i];
        }
        double max = scores[0];
        double min = scores[0];
        for (double d : scores) {
            if (d > max) {
                max = d;
            }
            else if (d < min) {
                min = d;
            }
        }
        System.out.println("平均分: " + sum / scores.length);
        System.out.println("最高分: " + max + " 最低分: " + min);

作業(yè)

  • 1.買彩票
    紅色球 1~33      藍色球 1~16
    01 05 11 17 18 22     05
    08 10 22 23 31 32    11
    數(shù)字不能重復(fù)
    輸入 幾 隨機選出 幾組號碼
        Scanner input = new Scanner(System.in);
        System.out.print("你想要幾組號碼,請輸入: ");
        int total = input.nextInt();
        for (int j = 0; j < total; j++) {
            int blueNum = (int) (Math.random() * 16 + 1);
            int[] num = new int[5];
            for (int i = 0; i < 5;) {
                int redNum = (int) (Math.random() * 33 + 1);
                if (!existed(num, redNum)) {
                    num[i] = redNum;
                     i++;
                }
            }
            bubbleSort(num);
            for (int i : num) {
                System.out.print(i + ",");
            }
            System.out.println(blueNum);
        }
        input.close();
    }
    
    public static void bubbleSort(int[] x) {
        boolean swapped = true;
        for (int i = 1; swapped && i <= x.length - 1; i++) {
            swapped = false;
            for (int j = 0; j < x.length - i; j++) {
                if (x[j] > x[j + 1]) {
                    int temp = x[j + 1];
                    x[j + 1] = x[j];
                    x[j] = temp;
                    swapped = true;
                }
            }
        }
    }
    
    public static boolean existed(int[] x, int a) {
        boolean flag = false;
        for (int i : x) {
            if (a == i) {
                flag = true;
                break;
            }
        }
        return flag;
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末库菲,一起剝皮案震驚了整個濱河市账忘,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌熙宇,老刑警劉巖鳖擒,帶你破解...
    沈念sama閱讀 216,997評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異烫止,居然都是意外死亡蒋荚,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評論 3 392
  • 文/潘曉璐 我一進店門烈拒,熙熙樓的掌柜王于貴愁眉苦臉地迎上來圆裕,“玉大人,你說我怎么就攤上這事荆几∠抛保” “怎么了?”我有些...
    開封第一講書人閱讀 163,359評論 0 353
  • 文/不壞的土叔 我叫張陵吨铸,是天一觀的道長行拢。 經(jīng)常有香客問我,道長诞吱,這世上最難降的妖魔是什么舟奠? 我笑而不...
    開封第一講書人閱讀 58,309評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮房维,結(jié)果婚禮上沼瘫,老公的妹妹穿的比我還像新娘。我一直安慰自己咙俩,他們只是感情好耿戚,可當(dāng)我...
    茶點故事閱讀 67,346評論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著阿趁,像睡著了一般膜蛔。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上脖阵,一...
    開封第一講書人閱讀 51,258評論 1 300
  • 那天皂股,我揣著相機與錄音,去河邊找鬼命黔。 笑死呜呐,一個胖子當(dāng)著我的面吹牛就斤,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播卵史,決...
    沈念sama閱讀 40,122評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼战转,長吁一口氣:“原來是場噩夢啊……” “哼搜立!你這毒婦竟也來了以躯?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,970評論 0 275
  • 序言:老撾萬榮一對情侶失蹤啄踊,失蹤者是張志新(化名)和其女友劉穎忧设,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體颠通,經(jīng)...
    沈念sama閱讀 45,403評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡址晕,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,596評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了顿锰。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片谨垃。...
    茶點故事閱讀 39,769評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖硼控,靈堂內(nèi)的尸體忽然破棺而出刘陶,到底是詐尸還是另有隱情,我是刑警寧澤牢撼,帶...
    沈念sama閱讀 35,464評論 5 344
  • 正文 年R本政府宣布匙隔,位于F島的核電站,受9級特大地震影響熏版,放射性物質(zhì)發(fā)生泄漏纷责。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,075評論 3 327
  • 文/蒙蒙 一撼短、第九天 我趴在偏房一處隱蔽的房頂上張望再膳。 院中可真熱鬧,春花似錦曲横、人聲如沸喂柒。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,705評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽胳喷。三九已至,卻和暖如春夭织,著一層夾襖步出監(jiān)牢的瞬間吭露,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,848評論 1 269
  • 我被黑心中介騙來泰國打工尊惰, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留讲竿,地道東北人泥兰。 一個月前我還...
    沈念sama閱讀 47,831評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像题禀,于是被迫代替她去往敵國和親鞋诗。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,678評論 2 354

推薦閱讀更多精彩內(nèi)容

  • 【程序1】 題目:古典問題:有一對兔子迈嘹,從出生后第3個月起每個月都生一對兔子削彬,小兔子長到第三個月后每個月又生一對兔...
    葉總韓閱讀 5,134評論 0 41
  • Java經(jīng)典問題算法大全 /*【程序1】 題目:古典問題:有一對兔子,從出生后第3個月起每個月都生一對兔子秀仲,小兔子...
    趙宇_阿特奇閱讀 1,863評論 0 2
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法融痛,類相關(guān)的語法,內(nèi)部類的語法神僵,繼承相關(guān)的語法雁刷,異常的語法,線程的語...
    子非魚_t_閱讀 31,625評論 18 399
  • 一保礼、 1沛励、請用Java寫一個冒泡排序方法 【參考答案】 public static void Bubble(int...
    獨云閱讀 1,369評論 0 6
  • 2013-07-03 智勇雙全才可自救 ——小城散漫表達系列之“智勇雙全” 火山 這個時代不缺乏智力支持,也不缺乏...
    朱明云閱讀 249評論 0 1