当前位置: 首页 > news >正文

java-方法的综合练习

一、买飞机票

        需求:

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

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

import java.util.Scanner;public class Main {public static void main(String[] args) {//键盘录入机票的价格、月份、头等舱或经济舱Scanner scanner = new Scanner(System.in);System.out.println("请输入机票的原价");int ticket = scanner.nextInt();System.out.println("请输入当前的月份");int month = scanner.nextInt();System.out.println("请输入当前购买的舱位(0:头等舱;1:经济舱)");int seat = scanner.nextInt();if (month >= 5 && month <= 10) {//旺季//先判断当前机票是经济舱还是头等舱if (seat == 0) {ticket = (int) (ticket * 0.9);} else if (seat == 1) {ticket = (int) (ticket * 0.85);} else {System.out.println("没有这个舱位");}} else if ((month >= 1 && month <= 4) || (month >= 11 && month <= 12)) {//淡季//先判断当前机票是经济舱还是头等舱if (seat == 0) {ticket = (int) (ticket * 0.7);} else if (seat == 1) {ticket = (int) (ticket * 0.65);} else {System.out.println("没有这个舱位");}} else {//键盘录入的月份不合法System.out.println("键盘录入的月份不合法");}//根据实际情况计算出对应的价格System.out.println("最终的价格是" + ticket);}
}

        优化后

 

import java.util.Scanner;public class Main {public static int getPrice(int ticket, int seat, 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;}public static void main(String[] args) {//键盘录入机票的价格、月份、头等舱或经济舱Scanner scanner = new Scanner(System.in);System.out.println("请输入机票的原价");int ticket = scanner.nextInt();System.out.println("请输入当前的月份");int month = scanner.nextInt();System.out.println("请输入当前购买的舱位(0:头等舱;1:经济舱)");int seat = scanner.nextInt();if (month >= 5 && month <= 10) {//旺季ticket = getPrice(ticket, seat, 0.9, 0.85);} else if ((month >= 1 && month <= 4) || (month >= 11 && month <= 12)) {//淡季ticket = getPrice(ticket, seat, 0.75, 0.65);} else {//键盘录入的月份不合法System.out.println("键盘录入的月份不合法");}//根据实际情况计算出对应的价格System.out.println("最终的价格是" + ticket);}
}

二、找质数

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

import java.util.Scanner;public class Main {public static void main(String[] args) {//判断101-200之间有多少个素数,并输出所有素数。int count = 0;for (int i = 101; i <= 200; i++) {//i表示循环的每一个数字//判断i是否是指数boolean flag = true;for (int j = 2; j < i; j++) {//j表示2-99之前的每一个数字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 Main {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));}}//随机抽取4次//随机抽取数组中的索引Random random = new Random();//定义变量 定义最总结果String result = "";for (int i = 0; i < 4; i++) {int randomIndex = random.nextInt(chs.length);result += chs[randomIndex];}//随机抽取数字int number =random.nextInt(10);//生成最终结果result+=number;//打印最终结果System.out.println(result);}
}

四、数组元素的复制

        需求:把一个数组中的元素复制到另一个新数组中去。

public class Main {public static void main(String[] args) {//定义一个老数组并存储一些元素int[] arr = {0, 1, 2, 3, 4};//定义一个新数组并长度与老数组一致int[] newArr = new int[arr.length];//遍历老数组,得到老数组中的每一个元素,一次存到新数组中for (int i = 0; i < arr.length; i++) {//i 表示老数组中的索引。新数组中的每一个索引//arr[i] 表示老数组中的元素newArr[i] = arr[i];}//依次打印新数组中的每个元素for (int i = 0; i < newArr.length; i++) {System.out.println(newArr[i]);}}
}

五、评委打分

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

import java.util.Scanner;public class Main {public static void main(String[] args) {//定义数组,计算6个评委的打分int[] scoreArr = getScores();//求出数组中的最大值int max = getMax(scoreArr);//求出数组中的最小值int min = getMin(scoreArr);//求出数组中6个分数的综合int sum=getSum(scoreArr);//(总和-最大值-最小值)/4int score=(sum-max-min)/(scoreArr.length-2);//打印结果System.out.println("最终得分为:"+score);}public static int[] getScores() {int[] scores = new int[6];//使用键盘录入的形式,输入分数0-100Scanner scanner = new Scanner(System.in);for (int i = 0; i < scores.length; ) {System.out.println("请输入评委的分数");int score = scanner.nextInt();if (score >= 0 && score <= 100) {scores[i] = score;i++;} else {System.out.println("成绩超出范围,请重新录入");}}return scores;}public static int getMax(int[] scoreArr) {int max = scoreArr[0];for (int i = 0; i < scoreArr.length; i++) {if (scoreArr[i] < max) {max = scoreArr[i];}}return max;}public static int getMin(int[] scoreArr) {int min = scoreArr[0];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 += scoreArr[i];}return sum;}
}

 

六、数字加密

七、数字解密

八、抢红包

九、模拟双色球(拓展)

http://www.dtcms.com/a/298799.html

相关文章:

  • 屏幕适配--像素篇
  • 100条常用SQL语句大全
  • Linux系统编程——进程
  • 两个MCU互联采集数据
  • kubesphere安装使用
  • 手写数组洗牌算法
  • Vue2 element cascader级联选择器懒加载编辑时回显数据
  • 【VLAs篇】06:从动作词元化视角谈VLA模型的综述
  • 异常(全)
  • which soffice soffice not found
  • Wordpress主题配置
  • 2025年7月24日·AI今日头条
  • KNN算法:从原理到实战全解析
  • Execel文档批量替换标签实现方案
  • day33:零基础学嵌入式之网络——TCP并发服务器
  • 基于javaweb的医院挂号系统
  • 动态规划解析:以最小花费爬楼梯为例
  • 纸板留声机:用ESP32和NFC打造会唱歌的复古装置
  • SeaweedFS深度解析(四):裸金属单机部署之配置文件启动master服务
  • IMU的精度对无人机姿态控制意味着什么?
  • [特殊字符] 第9篇:《SQL高阶 SELECT 技巧:DISTINCT、ORDER BY、LIMIT 全家桶》
  • ComfyUI中运行Wan 2.1工作流,电影级视频,兼容Mac, Windows
  • java微操
  • NLP验证自动化脚本优化
  • 硬核接线图+配置步骤:远程IO模块接入PLC全流程详解
  • 前端开发 Vue 状态优化
  • 多场景通用车辆计数算法助力暑期交通管理
  • Java从入门到精通!第十四天,重点!(反射)
  • 20250725-day22
  • Ivanti Endpoint Manager Mobile 远程命令执行漏洞复现(CVE-2025-4427)