Java基础的练习1

买飞机票

需求:
用户购买机票时,机票原价会按照淡季、旺季,头等舱还是经济舱的情况进行相应的优惠,优惠方案如下:5-10月为旺季,头等舱9折,经济舱8.5折;11月到来年4月为淡季,头等舱7折,经济舱6.5折,请开发程序计算出用户当前机票的优惠价。

import java.util.Scanner;

/**
 * 目标:
 *      完成买飞机票的案例
 */
public class Test01 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您的购票月份:");
        int month = sc.nextInt();
        System.out.println("请输入您的机票价格:");
        double price = sc.nextDouble();
        System.out.println("请选择您的称为类型:");
        System.out.println("经济舱 or 头等舱");
        String type = sc.next();
        double result = cal(month,price,type);
        System.out.println(result);
    }
    /**
     * 方法要做什么? 计算出用户当前机票的优惠价。
     * 方法的参数? 要, 月份、机票原价、舱位类型
     * 方法的返回值? 要, 优惠的价格  小数
     */
    public static double cal(int month, double price, String type){
        //定义一个变量 用于存储优惠的价格
        double calPrice = 0.0;
        //判断是淡季 还是 旺季
        if(month >= 5 && month <= 10){
            //旺季
            //判断机票的类型
            switch (type){
                case "头等舱":
                    calPrice = price * 0.9;
                    break;
                case "经济舱":
                    calPrice = price * 0.85;
                    break;
                default:
            }
        }else{
            //淡季
            switch (type){
                case "头等舱":
                    calPrice = price * 0.7;
                    break;
                case "经济舱":
                    calPrice = price * 0.65;
                    break;
                default:
            }
        }
        //直接返回计算出来的价格即可
        return calPrice;
    }
}

验证码

需求:

根据输入的需求开发指定位数的验证码,每位可以是数字,大写字母或小写字母。

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

public class Test02 {
    public static void main(String[] args) {
        // 需求:开发一个程序,可以生成指定位数的验证码,每位可以是数字、大小写字母。
        Scanner sc = new Scanner(System.in);
        System.out.println("请您输入您想生成的随机数位数:");
        int digit = sc.nextInt();
        String rs = getCode(digit);
        System.out.println(rs);
    }
    /**
     * 方法要做什么? 生成指定位数的验证码
     * 方法的参数? 要 验证码的长度 int
     * 方法的返回值? 要 验证码 String
     */
     public static String getCode(int length){
         //定义一个字符串 用于保存验证码
         String code = "";//String code;
         //创建一个随机数对象
         Random r = new Random();
         //在循环之前先产生一个随机数
         //随机到0则随机大写字母 随机到1则随机小写字母 随机到2则随机数字
         for (int i = 0; i < length; i++) {
             int num = r.nextInt(3);
             switch(num){
                 case 0:
                     int bigNum = r.nextInt(26) + 65;
                     char big = (char)bigNum;
                     code = code + big;
                     break;
                 case 1:
                     int smallNum = r.nextInt(26) + 97;
                     char small = (char)smallNum;
                     code = code + small;
                     break;
                 case 2:
                     int number = r.nextInt(10);
                     code = code + number;
                     break;
                 default:
             }
         }
         return code;
     }
}

评委打分

需求:

在唱歌比赛中,可能有多名评委要给选手打分,分数是[0 - 100]之间的整数。
选手最后得分为:去掉最高分、最低分后剩余分数的平均分,请编写程序能够录入多名评委的分数,并算出选手的最终得分。

import java.util.Scanner;

/**
 * 目标:完成评委打分案例。
 */
public class Test03 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入评委的人数:");
        int number = sc.nextInt();
        double rs = getAvg(number);
        System.out.println(rs);
    }
    /**
     * 方法要做的事情: 计算选手的最终得分
     * 方法的参数? 评委的人数
     * 方法的返回值? 最终得分 平均分
     */
    public static double getAvg(int count){
        double[] scores = new double[count];
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < count; i++) {
            System.out.println("请输入第"+(i+1)+"个评委的分数");
            //将键盘输入的分数 直接存储到数组
            scores[i] = sc.nextInt();
        }
        //在数组中找到 最大值 最小值 总分
        double max = scores[0];
        double min = scores[0];
        double sum = scores[0];
        for (int i = 1; i < scores.length; i++) {
            //先找最大值
            if(scores[i] > max){
                max = scores[i];
            }
            //再找最小值
            if (scores[i] < min){
                min = scores[i];
            }
            sum += scores[i];
        }
        return(sum - max - min)/(scores.length - 2);
    }
}

数组加密

需求:

某系统的数字密码是一个四位数,如1983,为了安全,需要加密后再传输,加密规则是:对密码中的每位数,都加5,再对10求余,最后将所有数字顺序反转,得到一串加密后的新数,请设计出满足本需求的加密程序!

public class Test04 {
    public static void main(String[] args) {
            String rs = encrypt(1983);
            System.out.println(rs);
        }
        /**
         * 方法要做的事情: 得到一串加密后的新数
         * 方法的参数? 给一个4位数 int
         * 方法的返回值? 加密之后的数据 String
         */
        public static String encrypt (int number){
            int[] arr = new int[4];
            arr[0] = number / 1000 % 10;
            arr[1] = number / 100 % 10;
            arr[2] = number / 100 % 10;
            arr[3] = number % 10;
            //循环加密 将每位数都进行同样的操作
            for (int i = 0; i < arr.length; i++) {
                arr[i] += 5;
                arr[i] %= 10;
            }
            //搞一个循环 对数组的元素进行反转
            int start = 0; int end = arr.length-1;
            while(start < end){
                int temp = arr[start];
                arr[start] = arr[end];
                arr[end] = temp;
                start++;
                end--;
            }
            //这里不能直接返回int[] arr,不然会直接打印出该数组的地址
            //再次循环将数组的元素进行拼接
            String num = "";
            for (int i = 0; i < arr.length; i++) {
                num = num + arr[i];
            }
            return num;
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值