黑马案例笔记

案例一 买飞机票

package com.liu.demo;

import java.util.Scanner;

public class Test1 {
    public static void main(String[] args) {
        //
        //
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入机票原价");
        double money = sc.nextDouble();

        System.out.println("请输入机票月份");
        int month = sc.nextInt();

        System.out.println("请选择仓位选择");
        String type = sc.next();

        //调用方法
        System.out.println("机票优惠后的价格是" + calc(money,month,type));
    }

    public static  double calc(double money,int month, String type ){
        if (month >= 5 && month <=10){
            switch (type){
                case "头等舱":
                    money *= 0.9;
                    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.7;
                    break;
                case "经济舱":
                    money *= 0.65;
                    break;
                default:
                    System.out.println("输入仓位类型有误");
                    money=-1;//表示当前无法计算价格

            }
        }else{
            System.out.println("输入的月份有误");
            money = -1;
        }
        return money;
    }
}
/*
请输入机票原价
1000
请输入机票月份
6
请选择仓位选择
经济舱
机票优惠后的价格是850.0

案例二 找素数

package com.liu.demo;
//找素数
public class Test02 {
    public static void main(String[] args) {

        //
        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");

            }
        }
    }
}
/*
101	103	107	109	113	127	131	137	139	149	151	157	163	167	173	179	181	191	193	197	199	

案例三 定义方法随机实现用一个5位的验证码,可以是数字,大写字母,小写字母。

package com.liu.demo;

import java.util.Random;

//定义方法随机实现用一个5位的验证码,可以是数字,大写字母,小写字母。
public class Test03 {
    public static void main(String[] args) {

        String code = creatCode(5);
        System.out.println(code);
    }
    //定义方法返回一个随机验证码, String ,声明形参 int n
    public static String creatCode(int n){
        String code = "";
        //for循环,循环n次
        Random r = new Random();
        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
                    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;
    }
}

数组元素的复制

package com.liu.demo;
//需求:把一个数组元素复制到另一个新数组中去
public class test04 {
    public static void main(String[] args) {
        int[] arr1 = {11, 22, 33, 44};
        int[] arr2 = new int[arr1.length];

        copy(arr1, arr2);
        printArray(arr1);
        printArray(arr2);
    }

    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.println("]");
    }
    public static void copy(int[] arr1, int[] arr2){

        for (int i = 0; i < arr1.length; i++) {
            arr2[i] = arr1[i];
        }
    }
}
/*
[11,22,33,44]
[11,22,33,44]
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值