java——编程案例

  • Post author:
  • Post category:java




案例1:买飞机票

在这里插入图片描述

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("请输入机票月份(1-12):");
        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.85;
                    break;
                default:
                    System.out.println("仓位类型输入有误!");
            }
        } 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("仓位类型输入有误!");
            }
        } else {
            System.out.println("输入错误!");
        }
        return money;
    }
}



案例2:找素数

在这里插入图片描述

public class Test2 {
    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 == true){
                System.out.print(i + "\t");
            }
        }
    }
}



案例3:开发验证码

在这里插入图片描述

import java.util.Random;
public class Test3 {
    public static void main(String[] args) {
        String code = createCode(5);
        System.out.println(code);
    }

    public static String createCode(int n){
        String code = "";
        Random r = new Random();
        for (int i = 0; i < n; i++) {
            //随机生成一个字符,英文大写、小写、数字  (0、1、2)
            int type = r.nextInt(3);
            switch (type){
                case 0:    //大写字符A-Z:65-90
                    char ch0 = (char) (r.nextInt(26) + 65);
                    code += ch0;
                    break;
                case 1:
                    char ch1 = (char) (r.nextInt(26) + 97);
                    code += ch1;
                    break;
                case 2:
                    code += r.nextInt(10);
                    break;
            }
        }
        return code;
    }
}



案例4:数组元素的复制

在这里插入图片描述

public class Test4 {
    public static void main(String[] args) {
        int[] arr1 = {11,22,33};
        int[] arr2 = new int[arr1.length];
        copy(arr1,arr2);
        printArray(arr1);
        printArray(arr2);
    }

    public static void copy(int[] arr1,int[] arr2){
        for (int i = 0; i < arr1.length; i++) {
            arr2[i] = arr1[i];
        }
    }
    
    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("]");
    }
}



案例5:评委打分

在这里插入图片描述

import java.util.Scanner;
public class Test5 {
    public static void main(String[] args) {
        int[] scores = new int[6];
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < scores.length; i++) {
            System.out.println("请您输入第" +(i+1)+"位评委的分数");
            int score = sc.nextInt();
            scores[i] = score;
        }
        
        int max = scores[0];
        int min = scores[0];
        int sum = scores[0];
        for (int i = 0; i < scores.length; i++) {
            if(scores[i] > max){
                max = scores[i];
            }
            if(scores[i] < min){
                min = scores[i];
            }
            sum += scores[i];
        }
        double result = (sum - max - min) *1.0/ (scores.length - 2);
        System.out.println("最终得分:" + result);
    }
}



案例6:数字加密

在这里插入图片描述

import java.util.Scanner;
public class Test6 {
    public static void main(String[] args) {
        //定义一个数组存入需要加密的数据
        System.out.println("请输入需要加密的数字个数:");
        Scanner sc = new Scanner(System.in);
        int length = sc.nextInt();
        int[] arr = new int[length];
        //录入需要加密的数字
        for (int i = 0; i < arr.length; i++) {
            System.out.println("请输入加密的第"+ (i+1) +"个数字");
            int number = sc.nextInt();
            arr[i] = number;
        }
        //打印数组内容看一下
        printArray(arr);
        //对数组中的数据加密
        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;
        }
        printArray(arr);
    }
    //打印数组的方法
    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("]");
    }
}



案例7:模拟双色球

在这里插入图片描述

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

public class Test7 {
    public static void main(String[] args) {
        int[] luckNumbers = createLuckNumber();
        int[] userNumbers = userInputNumbers();
        judge(luckNumbers,userNumbers);
    }
    //随机生成中奖号码:6个红球号码(1-33,不重复),随机1个蓝球号码(1-16)
    public static int[] createLuckNumber(){
        //定义一个动态数组
        int[] numbers = new int[7];
        //遍历数组,红球号码
        Random r = new Random();
        for (int i = 0; i < numbers.length-1; i++) {
            //注意:判断当前号码是否与之前的重复
            while (true){
                int data = r.nextInt(33) + 1;
                //定义一个flag变量,默认data无重复
                boolean flag = true;
                for (int j = 0; j < i; j++) {
                    if(numbers[j] == data){
                        flag = false;
                        break;
                    }
                }
                if(flag){
                    numbers[i] = data;
                    break;
                }
            }
        }
        //蓝球号码
        numbers[numbers.length - 1] = r.nextInt(16) + 1;
        return numbers;
    }

    //用户选号
    public static int[] userInputNumbers(){
        int[] numbers = new int[7];
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < numbers.length - 1; i++) {
            System.out.println("请输入第"+(i+1)+"个红球号码(1-33):");
             int data = sc.nextInt();
             numbers[i] = data;
        }
        System.out.println("请输入一个蓝球号码(1-16):");
        numbers[6] = sc.nextInt();
        return numbers;
    }

    //判断是否中奖,分别计算红球命中个数和篮球命中个数
    public static void judge(int[] luckNumbers,int[] userNumbers){
        //定义两个变量分别存储红球、蓝球的命中个数
        int redHit = 0;
        int blueHit = 0;
        //红球命中统计
        for (int i = 0; i < userNumbers.length - 1; i++) {
            for (int j = 0; j < luckNumbers.length - 1; j++) {
                if(userNumbers[i] == luckNumbers[j]){
                    redHit ++;
                    break;
                }
            }
        }
        //篮球命中统计
        if(userNumbers[6] == luckNumbers[6]){
            blueHit ++;
        }
        
        //结果显示
        System.out.println("中奖号码是:");
        printArray(luckNumbers);
        System.out.println("投注号码是:");
        printArray(userNumbers);
        System.out.println("命中红球个数:" + redHit);
        System.out.println("命中蓝球个数:" + blueHit);

        //判断中奖情况
        if(blueHit == 1 && redHit < 3){
            System.out.println("5元!");
        }else if(blueHit == 1 && redHit == 3 || blueHit== 0 && redHit == 4){
            System.out.println("10元!");
        }else if(blueHit == 1 && redHit == 4 || blueHit== 0 && redHit == 5){
            System.out.println("200元!");
        }else if(blueHit == 1 && redHit == 5){
            System.out.println("3000!");
        }else if(blueHit == 0 && redHit == 6){
            System.out.println("最高500万!");
        }else if(blueHit == 1 && redHit == 6){
            System.out.println("最高1000万!");
        }else {
            System.out.println("未中奖!");
        }
    }

    //打印数组
    public static void printArray(int[] arr){
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + "\t");
        }
        System.out.println();
    }
}



版权声明:本文为weixin_52135318原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。