• JAVA 12 联系回顾之前知识


    案例一 买飞机票

    import java.util.Scanner;
    
    public class FuncDemo6 {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入机票原价:double类型");
    
            double price = sc.nextDouble();
            boolean result=false;//false 淡季 true 旺季
            System.out.println("请输入月份:int类型");
            int month = sc.nextInt();
            //默认0 是经济舱 1是头等舱
            System.out.println("请输入机舱类型:int类型");
            int kind = sc.nextInt();
            dealMoney(month,result,kind,price);
        }
    
        public static void dealMoney(int month,boolean result,int kind,double price){
            if(month<0||month>12){
                System.out.println("月份有问题");
                return ;
            }
            //折扣
            double discount = 1.0;
            double discount1 = 1.0;
            //判断淡旺季 以及折扣比例
            if (month >= 5 && month <= 10) {
                result=true;
                discount = 0.9;
                discount1 = 0.85;
            } else if((month>0 &&month<5 )||(month>=10&&month<=12)){
                result=false;
                discount = 0.7;
                discount1 = 0.65;
            }
    
            //switch判断是头等舱还是经济舱
            switch(kind){
                case 0:
                    System.out.println("您的选择是"+"经济舱"+(result?discount:discount1)*price+"元"+(result?"旺季":"淡季"));
                    break;
                default:
                    System.out.println("您的选择是"+"头等舱"+(result?discount:discount1)*price+"元"+(result?"旺季":"淡季"));
                    break;
            }
        }
    }
    View Code

    案例二 求101 到200之间的素数

    public class FuncDemo7 {
        public static void main(String[] args) {
            //调用方法
            dealNum();
        }
    
        //求101-200之间的素数
        public static void dealNum(){
            //信号量
            boolean flag;
            for (int i = 101; i <200 ; i++) {
                //默认该元素是素数
                flag=true;
                for(int j=2;j<i/2;j++){
                    //能被取余没有余数则被整数 就不是素数
                    if(i%j==0){
                        //更改信号量
                        flag=false;
                        //跳出当前的一层循环
                        break;
                    }
                }
                //判断信号量
                if(flag){
                    System.out.print(i+"\t");
                }
    
            }
        }
    }
    View Code

    案例三 开发验证码

    import java.util.Random;
    
    public class FuncDemo9 {
        public static void main(String[] args) {
          String code= createCode(6);
            System.out.println(code);
        }
    
    
        //生成验证码方法 n位  大写字母 小写字母 数字
        public static String createCode(int n) {
            //字符串 存储
            String code = "";
            //实例化Random 类
            Random r = new Random();
            for (int i = 0; i < n; i++) {
                //num 随机生成 数字 大写 小写字母 三种中的一个
                int num = r.nextInt(3);
                switch (num) {
                    //随机生成数字
                    case 0:
                        code += r.nextInt(10);
                        break;
                    case 1:
                        //随机生成大写字母 大写字母ASCLL码范围65-90 在转换成字符
                        code += (char) (r.nextInt(25) + 65);
                        break;
                    //随机生成小写字母 大写字母ASCLL码范围97-122 在转换成字符
                    case 2:
                        code += (char) (r.nextInt(25) + 97);
                        break;
                }
            }
    
            return code;
        }
    }
    View Code

     案例四  数组元素的复制

    public class FuncDemo10 {
        public static void main(String[] args) {
            int [] arr={1,2,3,4,5,6,7};
            int [] arr1=new int[arr.length];
            copy(arr,arr1);
        }
    
        //数组的复制
        public static void copy(int[] arr1,int[] arr2){
            //正常完成元素的复制
            for (int i = 0; i < arr1.length; i++) {
               arr2[i] =arr1[i];
            }
            show(arr2);
        }
    
        //展示数组 形式[1,2,3,4,5,6]
        public static void show(int [] arr){
            if(arr.length>0 && arr!=null){
                System.out.print("[");
                for (int i = 0; i < arr.length; i++) {
                    if(i==arr.length-1){
                        System.out.print(arr[i]);
                    }else{
                        System.out.print(arr[i]+",");
                    }
                }
                System.out.print("]");
            }
        }
    }
    View Code

     案例五 求6个评委打分的平均分 去掉一个最高分 去掉一个最低分

    import java.util.Scanner;
    
    public class FuncDemo11 {
        public static void main(String[] args) {
            dealNum();
        }
    
        public static void dealNum() {
            //创建Scanner实例 并传递输入流对象
            Scanner sc = new Scanner(System.in);
            //动态生成6位教授打分数组
            int[] arr = new int[6];
            //临时变量保存
            int num;
            for (int i = 0; i < 6; i++) {
                System.out.println("第"+i+"个教授输入分");
                num = sc.nextInt();
                arr[i] = num;
            }
            showArr(arr);
            dealArr(arr);
        }
    
        //数组排序算法
        public static void dealArr(int[] arr) {
            int num;
            if (arr.length >= 2 && arr != null) {
                //冒泡排序
                for (int i = 0; i < arr.length - 1; i++) {
                    for (int j = 0; j < arr.length - i - 1; j++) {
                        if (arr[j] > arr[j + 1]) {
                            num = arr[j + 1];
                            arr[j + 1] = arr[j];
                            arr[j] = num;
                        }
                    }
                }
                showArr(arr);
    
                //求平均数
                getScores(arr);
            }
        }
        
        //展示 数组算法
        public static void showArr(int [] arr){
            if(arr.length>=0&&arr!=null){
                int len=arr.length-1;
                System.out.print("[");
                for (int i = 0; i < arr.length; i++) {
                    if(i==arr.length-1){
                        System.out.print(arr[i]);
                    }else{
                    System.out.print(arr[i]+",");
                    }
                }
                System.out.print("]");
            }
        }
    
    
        //去掉最高分 去掉最低分  求dd
        public static void getScores(int [] arr){
            int sum=0;
            arr[0]=0;
            arr[arr.length-1]=0;
            for (int i = 0; i < arr.length; i++) {
                sum+=arr[i];
            }
            //平均分
            int Scores=sum/4;
            System.out.println("最后平均得分"+Scores);
        }
    }
    View Code

     案例六 数字加密

    public class FuncDemo13 {
        public static void main(String[] args) {
            int [] arr={1,9,8,3};
            encryption(arr);
        }
    
        //加密
        public static void encryption(int[] arr) {
            //4为的数组长度  +5 在取余10
            for (int i = 0; i < arr.length; i++) {
                arr[i] = (arr[i] + 5) % 10;
            }
    
            //数组反转
            for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
                //定义临时变量 存储转换值
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
            showArray(arr);
        }
    
        //展示数组方法
        public static void showArray(int [] arr){
            System.out.print("[");
            for (int i = 0; i < arr.length; i++) {
                if(i==arr.length-1){
                    System.out.print(arr[i]);
                }else{
                    System.out.print(arr[i]+",");
                }
            }
            System.out.print("]");
        }
    }
    View Code
  • 相关阅读:
    C#网络编程(异步传输字符串)
    C#网络编程(同步传输字符串)
    C#网络编程(基本概念和操作)
    Asp.Net 构架(HttpModule 介绍)
    Asp.Net 构架(Http Handler 介绍)
    Asp.Net构架(Http请求处理流程)
    XML的应用 ---- 从一个范例看xml数据、xsd验证、xslt样式
    jQuery的ajax跨域实现
    常见26个jquery使用技巧详解
    常用Request对象获取请求信息
  • 原文地址:https://www.cnblogs.com/lvlisn/p/16398749.html
Copyright © 2020-2023  润新知