Java综合案例

  • 机票价格按照旺季淡季、头等舱和经济舱收费,输入机票原价、月份和头等舱或经济舱

        按照以下规则计算机票价格:旺季(5-10月)头等舱9折,经济舱8.5折,淡季(11月到来年4月)头等舱7折,经济舱6.5折

import java.util.Scanner;
public class index {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入机票的原价");
        int ticket = sc.nextInt();
        System.out.println("请输入月份");
        int month = sc.nextInt();
        System.out.println("请输入当前购买的舱位,0表示头等舱,1表示经济舱");
        int seat = sc.nextInt();

        if (month >= 5 && month <= 10){
            ticket = getPrice(seat,ticket,0.9,0.85);
        } else if ((month >= 1 && month < 5) || (month >= 11 && month <= 12)) {
            ticket = getPrice(seat,ticket,0.7,0.65);
        }else{
            System.out.println("你输入的月份不合格");
        }
        System.out.println("折扣后的机票价格为" + ticket +"元");
    }

    public static int getPrice(int seat,int ticket,double v0,double v1){
        if(seat == 0){
            ticket = (int)(ticket * v0);
        }else if(seat == 1){
            ticket = (int)(ticket * v1);
        }else{
            System.out.println("没有这个舱位");
        }
        return ticket;
    }
}
  • 判断101-200之间有多少素数,并输出所有素数
public class index {
    public static void main(String[] args) {
        int count = 0;
        //外循环,遍历101-200之内每一个数
        for (int i = 101; i <= 200; i++) {
            boolean flag = true;
            for (int j = 2; j < i; j++) {
                if (i % j == 0) {
                    flag = false;
                    //跳出内循环
                    break;
                }
            }
            if (flag) {
                System.out.println("当前数字" + i + "是质数");
                count++;
            }

        }
        System.out.println("一共有" + count + "个质数");

    }
}
  • 定义方法实现随机产生一个5位的验证码

        验证码格式:长度为5,前四位是大写字母或小写字母,最后一位是数字

import java.util.Random;
public class index {
    public static void main(String[] args) {
       char[] chs = new char[52];
       //大小写字母存放到数组中
        for (int i = 0; i < chs.length; i++) {
            //添加小写字母
            if(i <= 25){
                chs[i] = (char)(97 + i);
            }else {
                chs[i] = (char)(65 + i - 26);
            }
        }

        //定义一个字符串类型的变量,用来记录最终的结果
        String result = "";
        Random r = new Random();
        for (int i = 0; i < 4; i++) {
            int randomIndex = r.nextInt(chs.length);
            result = result + chs[randomIndex];
        }
        //获取随机数字0~9
        int number = r.nextInt(10);
        result = result + number;
        System.out.println(result);
    }
}
  • 把一个数组中的元素复制到另一个新数组中
public class index {
    public static void main(String[] args) {
       int[] arr = {1,2,3,4,5};
       //新数组的长度和老数组相同
       int[] newArr = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            newArr[i] = arr[i];
        }
        for (int i = 0; i < newArr.length; i++) {
            System.out.println(newArr[i]);
        }
    }
}
  • 在唱歌比赛中,有六位评委打分,分数范围是 [0-100] 之间的整数。选手的最终得分为:去掉最高分和最低分后4个评委的平均分,请完成上述过程并计算出选手的得分。
import java.util.Scanner;
public class index {
    public static void main(String[] args) {
        int[] scoreArr = getScores();
//        for (int i = 0; i < scoreArr.length; i++) {
//            System.out.println(scoreArr[i]);
//        }
        int max = getMax(scoreArr);
        int min = getMin(scoreArr);
        int sum = getSum(scoreArr);
        int avg = (sum - max -min) / (scoreArr.length - 2);
        System.out.println("选手的最终得分是"+avg+"分");
    }

    public static int[] getScores(){
        //动态定义数组
        int[] scores = new int[6];
        Scanner sc = new Scanner(System.in);

        for (int i = 0; i < scores.length;) {
            System.out.println("请输入第"+(i + 1)+"位评委的打分:");
            int score = sc.nextInt();
            //判断是否在0-100范围内
            if(score >= 0 && score <= 100){
                scores[i] = score;
                //只有输入合法的分数才能进入if判断语句,i才加1,才能进入下一次循环
                i++;
            }else {
                System.out.println("成绩超出了范围,继续录入,当前i为" + i);
            }
        }
        return scores;
    }

    //求数组的最大值
    public static int getMax(int[] scoreArr){
        int max = scoreArr[0];
        for (int i = 1; i < scoreArr.length; i++) {
            if (scoreArr[i] > max){
                max = scoreArr[i];
            }
        }
        return max;
    }


    //求数组的最小值
    public static int getMin(int[] scoreArr){
        int min = scoreArr[0];
        //循环从0开始,第一次自己跟自己比较,可以从1开始
        for (int i = 0; i < scoreArr.length; i++) {
            if (scoreArr[i] < min){
                min = scoreArr[i];
            }
        }
        return min;
    }

    //求总分数
    public static int getSum(int[] scoreArr){
        int sum = 0;
        for (int i = 0; i < scoreArr.length; i++) {
            sum = sum + scoreArr[i];
        }
        return sum;
    }
}

某系统的数字密码(大于0),采用加密方式进行传输

规则如下:先得到每位数,如何每位数都加上5,再对10求余,最后将所有数字反转,得到一串新数

public class index {
    public static void main(String[] args) {
        int[] arr = getNumber();
        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;
        }

        //拼接
        int number = 0;
        for (int i = 0; i < arr.length; i++) {
            number = number * 10 + arr[i];
        }
        System.out.println(number);
    }

    public static int[] getNumber(){
        //把整数上每一位添加到数组中
        int number = 1983;
        int temp = number;
        int count = 0;
        while(number != 0){
            number = number / 10;
            count++;
        }
        int[] arr = new int[count];
        //索引
        int index = arr.length - 1;
        while(temp != 0){
            int ge = temp % 10;
            temp = temp / 10;
            arr[index] = ge;
            index--;
        }
        return arr;
    }


}
  • 数据解密:

        加密前数据取值范围:1 2 3 4  5   6   7   8   9

                +5                    : 6 7 8 9 10 11 12 13 14

                %10                 : 6 7 8 9   0  1   2   3   4

public class index {
    public static void main(String[] args) {
        int[] arr = getNumber();
        //反转
        for (int i = 0,j=arr.length-1; i < j; i++,j--) {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] > 5 && arr[i] <= 9){
                arr[i] = arr[i] - 5;
            }else if(arr[i] >= 0 && arr[i] <= 4){
                arr[i] = (arr[i] + 10) - 5;
            }
        }
        //拼接
        int number = 0;
        for (int i = 0; i < arr.length; i++) {
            number = number * 10 + arr[i];
        }
        System.out.println(number);
    }

    public static int[] getNumber(){
        //把整数上每一位添加到数组中
        int number = 8346;
        int temp = number;
        int count = 0;
        while(number != 0){
            number = number / 10;
            count++;
        }
        int[] arr = new int[count];
        //索引
        int index = arr.length - 1;
        while(temp != 0){
            int ge = temp % 10;
            temp = temp / 10;
            arr[index] = ge;
            index--;
        }
        return arr;
    }


}
  • 直播抽奖,奖金是现金红包,分别有{2,588,888,1000,10000}五个奖金,请使用代码模拟抽奖,打印每一个奖项,奖项的出现顺序随机且不重复。

方法1:

import java.util.Random;
import java.util.Scanner;
public class index {
    public static void main(String[] args) {
        int[] arr = {2,588,888,1000,10000};
        //新数组存储抽奖结果
        int[] newArr = new int[arr.length];
        Random r = new Random();
        for (int i = 0; i < 5;) {
            int randomIndex = r.nextInt(arr.length);
            int prize = arr[randomIndex];
            boolean flag = contains(newArr,prize);
            if (!flag){
                newArr[i] = prize;
                i++;
            }
        }
        for (int i = 0; i < newArr.length; i++) {
            System.out.println(newArr[i] );
        }
    }

    public static boolean contains(int[] arr,int prize){
        //判断抽奖结果在数组中是否存在
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == prize){
                return true;
            }
        }
        return false;
    }


}

方法2:

import java.util.Random;
import java.util.Scanner;
public class index {
    public static void main(String[] args) {
        int[] arr = {2,588,888,1000,10000};
        Random r = new Random();
        for (int i = 0; i < arr.length;i++) {
            int randomIndex = r.nextInt(arr.length);
            int temp = arr[i];
            arr[i] = arr[randomIndex];
            arr[randomIndex] = temp;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i] );
        }
    }
}
  • 模拟双色球系统

import java.util.Random;
import java.util.Scanner;
public class index {
    public static void main(String[] args) {
        //生成中奖号码
        int[] arr = creatNumber();

        System.out.print("这期双色球的中奖号码有");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println("");

        //用户输入号码
        int[] userInputArr = userInputNumber();

        //判断用户中奖情况
        int redCount = 0;
        int blueCount = 0;

        for (int i = 0; i < userInputArr.length - 1; i++) {
            int redNumber = userInputArr[i];
            for (int j = 0; j < arr.length - 1; j++) {
                if (redNumber == arr[i]){
                    redCount++;
                    //如果找到了,跳出内循环,判断下一个红球号码是否中奖
                    break;
                }
            }
        }
        //蓝球
        int blueNumber = userInputArr[userInputArr.length - 1];
        if (blueNumber == arr[arr.length - 1]){
            blueCount++;
        }

        //判断中奖情况
        if (redCount == 6 && blueCount == 1){
            System.out.println("恭喜你,中了一等奖1000万");
        }else if (redCount == 6 && blueCount == 0){
            System.out.println("恭喜你,中了二等奖500万");
        }else if (redCount == 5 && blueCount == 1){
            System.out.println("恭喜你,中了三等奖奖3000元");
        }else if ((redCount == 5 && blueCount == 0) || (redCount == 4 && blueCount == 1)){
            System.out.println("恭喜你,中了四等奖200元");
        }else if ((redCount == 4 && blueCount == 0) || (redCount == 3 && blueCount == 1)){
            System.out.println("恭喜你,中了五等奖10元");
        }else if ((redCount == 2 && blueCount == 1) || (redCount == 1 && blueCount == 1) || (redCount == 0 && blueCount == 1)){
            System.out.println("恭喜你,中了六等奖5元");
        }else {
            System.out.println("谢谢参与");
        }
    }

    public static int[] userInputNumber(){
        //创建数组用于添加用户购买的彩票号码
        int[] arr = new int[7];

        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < 6;) {
            System.out.println("请输入第" + (i + 1) + "个红球号码");
            int redNumber = sc.nextInt();
            if (redNumber >= 1 && redNumber <= 33){
                boolean flag = contains(arr,redNumber);
                if (!flag){
                    //不存在,可以添加到数组
                    arr[i] = redNumber;
                    i++;
                }else{
                    System.out.println("当前红球号码已存在,请重新输入");
                }
            }else{
                System.out.println("当前红球号码超出范围");
            }
        }

        //蓝球
        System.out.println("请输入蓝球号码:");
        while (true) {
            int blueNumber = sc.nextInt();
            if (blueNumber >= 1 && blueNumber <= 16){
                arr[arr.length-1] = blueNumber;
                break;
            }else {
                System.out.println("当前蓝球号码不符合要求");
            }
        }
        return arr;
    }

    public static int[] creatNumber(){
        //创建数组用于添加中奖号码
        int[] arr = new int[7];

        //红球号码不能重复,篮球可以跟红球号码重复
        Random r = new Random();
        for (int i = 0; i < 6;) {
            //获取红球号码
            int rednumber = r.nextInt(33) + 1;
            boolean flag = contains(arr,rednumber);
            if (!flag){
                arr[i] = rednumber;
                i++;
            }
        }
        //篮球号码
        int buleNumber = r.nextInt(16) + 1;
        arr[arr.length - 1] = buleNumber;

        return arr;
    }

    //用于判断数字在数组中是否存在
    public static boolean contains(int[] arr,int number){
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == number){
                return true;
            }
        }
        return false;
    }
}
  • 某商场每个季度的营业额如下:单位(万元)

                第一季度:22,66,44

                第二季度:77,33,88

                第三季度:24,45,65

                第四季度:11,66,99

要求计算出每个季度的总营业额和全年的总营业额

public class text {

    public static void main(String[] args) {
        int[][] yearArrArr = {
                {22, 66, 44},
                {77, 33, 88},
                {24, 45, 65},
                {11, 66, 99}
        };

        int yearSum = 0;
        //遍历二维数组得到每一个一维数组
        for (int i = 0; i < yearArrArr.length; i++) {
            int [] quarterArr = yearArrArr[i];
            int sum = getSum(quarterArr);
            System.out.println("第" + (i+1) + "个季度的营业额为" + sum);
            yearSum = yearSum + sum;
        }
        System.out.println("全年总营业额为" + yearSum);
    }

    //定义一个方法,用来计算每个季度的营业额
    public static int getSum(int[] arr){
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum = sum + arr[i];
        }
        return sum;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值