Java基础案例集

2022/2/8 9:42:36

本文主要是介绍Java基础案例集,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

目录

买飞机票

找素数

验证码生成

评委打分

数字加密


买飞机票

需求:机票价格按照淡季旺季、头等舱和经济舱收费、输入机票原价、月份和头等舱或经济舱。
按照如下规则计算机票价格:旺季(5-10月)头等舱9折,经济舱8.5折,淡季(11月到来年4月)头等舱7折,经济舱6.5折。

package com.beijing.test;

import java.util.Scanner;

public class HelloWorld {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //输入机票
        System.out.print("请输入机票原价:");
        double price = sc.nextDouble();
        //输入月份
        System.out.print("请输入月份:");
        int month = sc.nextInt();
        //输入仓位类型
        System.out.print("请输入仓位类型(头等舱或经济舱):");
        String type = sc.next();
        //调用函数进行计算
        double new_price = calc(price, month, type);
        System.out.println("打折后的机票价格为:"+new_price);

    }
    /*
        定义一个方法用来接收 机票原价、月份、头等或者经济舱
    * */
    public static double calc(double price, int month, String type ){
        //判断月份大小,如果是旺季
        if(month >= 5 && month <= 10){
            //接着判断是经济舱还是头等舱,因为判断的是具体的值,所以用switch
            switch (type){
                case "经济舱":
                    price *= 0.85;
                    break;
                case "头等舱":
                    price *= 0.9;
                    break;
                default:
                    System.out.println("您输入的仓位不正常");
                    price = -1;
            }
        //如果是淡季
        } else if(month == 11 || month == 12 || month >= 1 & month <= 4){
            switch (type) {
                case "经济舱":
                    price *= 0.65;
                    break;
                case "头等舱":
                    price *= 0.7;
                    break;
                default:
                    System.out.println("您输入的仓位不正常");
                    price = -1;
            }
        }else{
            System.out.println("您输入的月份有误");
            price = -1;
        }
        return price;
    }

}

总结:

  1. 如果判断的是一个范围用if;如果判断的数据为几个具体的值,则用switch
  2. case里面记得加break
  3. 了解函数的写法,以及函数的调用方式

找素数

素数,如果除了1和它本身以外,不能被其他正整数整除,就叫素数。

要求:判断101-200之间有多少个素数,并输出所有素数

package com.beijing.test;

import java.util.Scanner;

public class HelloWorld {
    public static void main(String[] args) {
        for (int i = 101; i <= 200 ; i++) {
            boolean flag = true;
            for (int j = 2; j < i / 2 ; j++) {
                //对其取余,如果余数为0,说明能被整数,即不是素数
                if (i % j == 0){
                    flag = false;   //说明不是素数
                    break;
                }
            }
            if(flag){   //说明是素数
                System.out.print(i + ", ");
            }
        }

    }
    
}

总结:

  1. 如何找出素数?我们可以用这个数依次从2开始除,除到这个数的一半就行,如果余数一直不为0,说明为素数
  2. java中除法会默认取整,如 5/2=3

验证码生成

需求:定义方法实现随机产生一个5位的验证码,每位可能是数字、大写字母、小写字母。

思考:要求产生5位的随机验证码,则可以让每一位都进行随机,然而随机的情况有三种:大写字母、小写字母、数字。我们可以套用两层随机,一层随机是字母还是数字类型。另一层随机成具体的数据

package com.beijing.test;

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

public class HelloWorld {
    public static void main(String[] args) {
        //3. 调用函数
        String code = createCode(5);
        System.out.println(code);

    }
    public static String createCode(int n){
        //1. 定义字符串用来记录随机验证码
        String code = "";
        Random r = new Random();
        //2. 定义for循环,生成n位验证码
        for (int i = 0; i < n; i++) {
            //生成一个随机字符:大小字母,小写字母,数字
            int type = r.nextInt(3); //0,1,2
            switch (type){
                case 0:
                    //大写字符(A - Z 65 + 26)
                    char ch = (char)(r.nextInt(26) + 65);
                    code += ch;
                    break;
                case 1:
                    //小写字符(a - z 97 + 26)
                    char ch1 = (char)(r.nextInt(26) + 97);
                    code += ch1;
                    break;
                case 2:
                    //数字
                    code += r.nextInt(10);  //0-9
                    break;
            }

        }
        return code;

    }

}

总结:

  • 将string类型和int类型相加,起的是字符串拼接的作用;
  • 将char类型和int类型相加,使用的是char类型对应的ascii数字来进行相加
  • 把数据范围大的变量赋值给数据范围小的变量需要将数据范围大的变量进行强制类型转换

评委打分

需求:在唱歌比赛中,有6名评委给选手打分,分数范围是[0-100]之间的整数。选手的最后得分为去掉最高分、最低分后的4个评委的平均分,请完成上述过程并计算出选手的得分。

分析:

  • 用动态初始化数组的方式来录入分数
  • 遍历数组找出最高分和最低分及总分
package com.beijing.test;

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

public class HelloWorld {
    public static void main(String[] args) {
        //1. 定义一个动态初始化数组用于接收分数
        int[] scores = new int[6];

        //2. 录入分数
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < scores.length; i++) {
            System.out.print("请输入第"+i+1+"个评委的打分: ");
            int score = sc.nextInt();
            //将分数存入到数组对应的位置
            scores[i] = score;
        }

        //3. 遍历数组找出最大值,最小值,总分
        int max = scores[0];
        int min = scores[0];
        int sum = 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];
        }
        System.out.println("最高分为:"+max);
        System.out.println("最低分为:"+min);
        //4. 求平均分
        double result = (sum - max - min)/ (scores.length -2);
        System.out.println("最后得分为:" + result);
    }
}

数字加密

某系统的数字密码︰比如1983,采用加密方式进行传输,规则如下︰先得到每位数,然后每位数都加上5,再对10求余,最后将所有数字反转,得到一串新数。

package com.beijing.test;

import java.util.Scanner;

public class HelloWorld {
    public static void main(String[] args) {
        //1.定义一个数组存入需要加密的数据
        System.out.print("请输入您要加密的数字个数:");
        Scanner sc = new Scanner(System.in);
        int length = sc.nextInt();
        int[] arr = new int[length];

        //2. 录入要加密的数字并存入到数组中
        for (int i = 0; i < arr.length; i++) {
            System.out.print("请输入要加密的第"+(i + 1)+"个数字:");
            int number = sc.nextInt();
            arr[i] = number;
        }
        //3.打印数据内容看一下
        System.out.println("要加密的数据为:");
        printArray(arr);

        //4.对数组中的数据内容进行加密
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (arr[i] + 5) % 10;
        }
        //5.对数组中加密的内容进行反转
        for (int i = 0, j = arr.length - 1; i < j; i++, j--){
            //直接交换两者位置的值
            int temp = arr[j];
            arr[j] = arr[i];
            arr[i] = temp;
        }
        //打印最后加密的结果看看
        System.out.println("最终加密后的数据为:");
        printArray(arr);

    }
    //定义打印数组的方法
    public static void printArray(int[] arr){
        System.out.print("[");
        if (arr != null && arr.length > 0){
            for (int i = 0; i < arr.length; i++) {
                System.out.print(i == arr.length - 1 ? arr[i] : arr[i] + ",");
            }
        }
        System.out.println("]");
    }
    }

总结:

1. 知道如何对数组元素进行反转。定义两个变量分别占数组的首尾位置,一个变量往前走,一个变量往后走,同步交换双方位置处的值



这篇关于Java基础案例集的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程