JAVA基礎 - 案例練習

一、前言

  前面學了點基礎,可以通過一些小案例鞏固下基礎

二、實例

package com.hello.base;

import java.util.Random;
import java.util.Scanner;

public class CaseDemo {
    public static void main(String[] args) {
        //錄入購買信息,調用方法得到最終結果
        Scanner sc = new Scanner(System.in);
        System.out.println("機票原價");
        double price = sc.nextDouble();
        System.out.println("月份");
        int month = sc.nextInt();
        System.out.println("倉位類型(頭等艙、經濟艙)");
        String type = sc.next();

        double rs = calc(price,month,type);
        System.out.println("您當前購買機票的價格是:"+rs);

        //調用尋找素數
        su();
        System.out.print("\n");

        //調用生成驗證碼的方法
        String code = CreateCode(5);
        System.out.println("隨機驗證碼:"+code);

        //調用元素複製方法
        int [] arrc1 = {11,22,44};
        int [] arrc2 = new int[arrc1.length];
        copy(arrc1,arrc2);//此時已完成數組複製,如果想打印出來具體的值,需要再寫一個方法
        printArray(arrc1);
        printArray(arrc2);
        System.out.println("\n");

        //調用計算評委得分
        int [] arrs = new int[6];//定義一個動態初始化的數據,用於後期錄入6個分數
        Scanner scs = new Scanner(System.in);
        for(int i=0;i<arrs.length;i++){
            System.out.println("請錄入第"+(i+1)+"個分數");
            int scores = scs.nextInt();
            arrs[i] = scores;//把這個分數存入到數組對應的位置
        }
        calcscore(arrs);

    }
    //打印複製元素的值的方法
    public static void printArray(int[] arr){
        System.out.print("[");
        for(int i=0;i<arr.length;i++){
            System.out.print(i==arr.length-1?arr[i]:arr[i]+",");
        }
        System.out.print("]");
    }


    /*案例一:買機票
1、機票價格按照淡季旺季、頭等艙和經濟艙收費、輸入機票原價、月份和頭等艙或經濟艙
2、按照如下規則計算機票價格:旺季(5-10月)頭等艙9折,經濟艙8.5折,淡季(11月到來年4月)頭等艙7折,經濟艙6.5折
注:遇到判斷值匹配的時候使用switch分支結構實現;遇到判斷區間範圍的時候選擇if分支結構實現
 */
    public static double calc(double money,int month,String type) {
        //判斷月份是淡季還是旺季
        if (month >= 5 && month <= 10) {
            //旺季
            switch (type) {
                case "經濟艙":
                    money *= 0.85;
                    break;
                case "頭等艙":
                    money *= 0.9;
                    break;
                default:
                    System.out.println("您輸入的艙位不正確");
                    money = -1;//當前無法計算價格了
            }
        } else if (month == 11 || month == 12 || month >= 1 && month <= 4) {
            //淡季
            switch (type) {
                case "經濟艙":
                    money *= 0.65;
                    break;
                case "頭等艙":
                    money *= 0.7;
                    break;
                default:
                    System.out.println("您輸入的艙位不正確");
                    money = -1;//當前無法計算價格了
            }
        } else {
            System.out.println("月份有問題");
            money = -1;
        }
        return money;
    }
    /*
    案例二:找素數(如果除了1和它本身以外,不能被其他正整數整除,就叫素數)
     */
    public static void su(){
        //定義一個循環,找到101-200之間的全部數據
        for(int i =101;i<=200;i++){
            //信號位:標記
            boolean flag = true;//一開始認爲當前數據是素數
            //判斷當前遍歷的這個數是否是素數
            for(int j=2;j<i/2;j++){
                if(i%j==0){
                    flag=false;
                    break;
                }
            }
            //根據判斷的結果選擇是否輸出這個數據,是素數則輸出
            if(flag){
                System.out.print(i+"\t");
            }
        }
    }
    /*
    案例三:開發驗證碼:定義方法實現隨機產生一個5位的驗證碼,每位可能是數字、大寫字母、小寫字母
    核心邏輯:
    1、定義一個string類型的變量存儲驗證碼字符
    2、定義一個for循環,循環5次
    3、隨機生成0|1|2的數據,依次代表當前位置要生成數字|大寫字母|小寫字母
    4、把0、1、2交給switch生成對應類型的隨機字符,把字符交給string變量
    5、循環結束後,返回string類型的變量即是所求的驗證碼結果。
     */
    public static String CreateCode(int n){
        //定義一個字符串變量記錄生成的隨機字符
        String code="";
        Random r = new Random();
        //定義一個for循環,循環n次,依次生成隨機字符
        for(int i=0;i<n;i++){
            //生成一個隨機字符,英文大寫 小寫 數字(0 1 2)
            int type = r.nextInt(3);//0 1 2
            switch (type){
                case 0:
                    //大寫字符(A 65 - Z 65+25) (0-25)+65
                    //注:A-Z對應的數字是65-90,生成隨機數65-90的寫法爲nextInt(26)+65
                    //減加法 90-65=25,生成25個數即(0-26),括號裏寫26,外面+65,即隨機數從65開始,至65+25=90結束
                    char ch = (char) (r.nextInt(26)+65);
                    code +=ch;
                    break;
                case 1:
                    //小寫字符(a 97 - z 97+25) (0-25)+97
                    char ch1 = (char) (r.nextInt(26)+97);
                    code +=ch1;
                    break;
                case 2:
                    //數字字符
                    code += r.nextInt(10);//0-9
                    break;
            }
        }
        return code;
    }
    /*
    案例四:數組元素複製:把一個數組中的元素複製到另一個新數組去
    分析:1、需要動態初始化一個數組,長度與原數組一樣;2、遍歷原數組的每個元素,依次賦值給新數組;3、輸出兩個數組的內容
     */
    public static void copy(int arrc1[],int arrc2[]){
        //完成元素的複製
        for(int i=0;i<arrc1.length;i++){
            arrc2[i]=arrc1[i];
        }
    }
    /*
    案例五:評委打分:有6名評委給選手打分,分數範圍是[0-100]之間的整數,選手的最後得分爲去掉最高分、最低分後的4個評委的平均分,
    請完成上述過程並計算出選手的得分
    分析:1、把6個評委的分數錄入到程序中去---->使用數組  int[] scores = new int[6];
    2、遍歷數組中每個數據,進行累加求和,並找出最高分、最低分
    3、按照分數的計算規則算出平均分。
     */
    public static void calcscore(int[] arrs){
        //遍歷數組中的每個數據,找出最高分、最低分、總分
        int max=arrs[0], min =arrs[0], sum=0;
        for (int i=0;i<arrs.length;i++){
            if(arrs[i]>max){
                max=arrs[i];//替換最大值變量存儲的數據
            }
            if(arrs[i]<min){
                min=arrs[i];//替換最小值變量存儲的數據
            }
        sum +=arrs[i];//  統計總分
        }
        System.out.println("最高分爲:"+max);
        System.out.println("最低分爲:"+min);
        double result = (sum-max-min)/(arrs.length-2);
        System.out.println("最終得分爲:"+result);

    }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章