学习打卡day06-编程思维训练
注意:在定义方法的时候首先思考方法的参数列表需不需要,需要的话,什么类型?方法的返回值需不需要,需要的话,什么数据类型,然后在编写方法。
下面的练习尽量的定义在一个独立的方法中!
练习 1: 买飞机票
需求:
用户购买机票时,机票原价会按照淡季、旺季,头等舱还是经济舱的情况进行相应的优惠,优惠方案如下:5-10月为旺季,头等舱9折,经济舱8.5折; 11月到来年4月为淡季,头等舱7折,经济舱6.5折,请开发程序计算出用户当前机票的优惠价。
分析:
方法是否需要接收数据? 需要接收机票原价、当前月份、舱位类型;
方法是否需要返回数据?需要返回计算出的机票优惠价。
方法内部:先使用if判断月份是旺季还是淡季,然后使用switch分支判断是头等舱还是经济舱
代码实现:
package com.yfs1024.homework;
/**
* Author : Cookie
* Date : 2023/2/15 19:05
* Explain :
* 用户购买机票时,机票原价会按照淡季、旺季,头等舱还是经济舱的情况进行相应的优惠,
* 优惠方案如下:5-10月为旺季,头等舱9折,经济舱8.5折; 11月到来年4月为淡季,头等舱7折,经济舱6.5折,
* 请开发程序计算出用户当前机票的优惠价。
*/
public class BuyAirTickets {
public static void main(String[] args) {
System.out.println(returnFinalPrice(1000,3,"头等舱"));
}
public static double returnFinalPrice(double price, int month, String cabinType) {
// 首先排除非法的情况,即价格为负数,月份不是1-12,舱型不是经济舱和头等舱 ,这里咱们主要注重代码,先暂时放下非法
if (month >= 5 && month <= 10) {
// 旺季 , 由于还没有学习字符串的比较,所以这里先用switch, 因为switch支持五种类型: byte,short,int,char,String
switch (cabinType) {
case "头等舱":
// 这里有了return方法直接结束,所以下面的break就不用在写
return price * 0.9;
case "经济舱":
return price * 0.85;
default:
return 0.0;
}
} else {
// 淡季
switch (cabinType) {
case "头等舱":
// 这里有了return方法直接结束,所以下面的break就不用在写
return price * 0.7;
case "经济舱":
return price * 0.65;
default:
return 0.0;
}
}
}
}
练习 2: 开发验证码
需求: 开发一个程序,可以生成指定位数的验证码,每位可以是数字、大小写字母。
这里有两种思路: 一种是直接生成再拼接,一种是将对应的数据,通过代码填充到数组中,然后随机数获取再拼接
补充知识:
0-10 的Ascll码为 48 - 57
A-Z 的Ascll码为 65 - 90
a-z 的Ascll码为 97-122
示例代码如下:
package com.yfs1024.homework;
import java.util.Random;
/**
* Author : Cookie
* Date : 2023/2/15 19:21
* Explain :
* 开发一个程序,可以生成指定位数的验证码,每位可以是数字、大小写字母。
* 分析: 因为是指定位数,所以参数肯定是int类型的,返回值,肯定是验证码
*/
public class GenerateVerificationCode {
public static void main(String[] args) {
// 这里生成一个五位数的验证码
System.out.println(returnVerificationCode(5));
}
public static String returnVerificationCode(int num) {
Random random = new Random();
// 定义一个字符串用于拼接和返回
String str = "";
for (int i = 0; i < num; i++) {
// 因为有三种类型: 数字,大小写字母,在这里用0,1,2来分别代表
int code = random.nextInt(3);
// 数字
if (code == 0) {
int number = random.nextInt(10);
str += number;
// 大写字母
} else if (code == 1) {
int number2 = random.nextInt(65, 91);
str += (char)number2;
// 小写字母
} else {
int number3 = random.nextInt(97, 123);
str += (char)number3;
}
}
return str;
}
}
练习 3: 数组拷贝
需求: 请把一个整型数组,例如存了数据:11,22,33,拷贝成一个一模一样的新数组出来。
分析在代码中!
package com.yfs1024.homework;
/**
* Author : Cookie
* Date : 2023/2/15 19:36
* Explain :
* 请把一个整型数组,例如存了数据:11,22,33,拷贝成一个一模一样的新数组出来
* 分析:
* 这里主要是想让把堆中的数据进行拷贝,并不是简单的地址拷贝, 很明显,参数为一个数组,返回值为拷贝之后的数组
*/
public class ArrayCopy {
public static void main(String[] args) {
int[] arr = {1, 23, 45};
int[] ints = arrayCopy(arr);
System.out.println("拷贝之前的地址:" + arr);
for (int num : arr) {
System.out.println(num);
}
System.out.println("拷贝之后的地址:" + ints);
for (int num : ints) {
System.out.println(num);
}
}
public static int[] arrayCopy(int[] arr) {
// 排空处理
if (arr == null) {
return null;
}
int[] arr1 = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
arr1[i] = arr[i];
}
return arr1;
}
}
练习 4:抢红包
需求:
一个大V直播时发起了抢红包活动,分别有:9、666、188、520、99999五个红包。 请模拟粉丝来抽奖,按照先来先得,随机抽取,抽完即止,注意:一个红包只能被抽一次,先抽或后抽哪一个红包是随机的,示例如下(不一定是下面的顺序):
分析: 这里的方法需要提供的参数,为红包的数组,无返回值
package com.yfs1024.homework;
import java.util.Random;
import java.util.Scanner;
/**
* Author : Cookie
* Date : 2023/2/15 20:36
* Explain :
*/
public class RedHongBao {
public static void main(String[] args) {
int[] arr = {11, 22, 666, 999};
getMoney(arr);
}
//设计该方法用于进行抢红包行为
public static void getMoney(int[] arr) {
Scanner sc = new Scanner(System.in);
Random random = new Random();
for (int i = 0; i < arr.length; i++) {
System.out.println("请按任意键进行抽奖:");
// 因为使用这里的数据,所以不用接收
sc.next();
// 死循环进行抽奖的处理,如果抽中就把当前索引的数据设置为0 ,
// 如果没有抽中就一直循环直到抽中为止
while (true) {
// 随机找到抽奖的位置
int index = random.nextInt(arr.length);
if (arr[index] != 0) {
// 说明抽中
System.out.println("您是第" + (i + 1) + "抽奖者!" + "恭喜您您抽中了" + arr[index]);
arr[index] = 0;
break;
}
}
}
System.out.println("抽奖结束!");
}
}
练习5: 找素数
除了1和它本身以外,不能被其他正整数整除,就叫素数。比如:3、7就是素数,而9、21等等不是素数。
分析: 这里可以设计两个方法, 第一个是: 判断一个数是不是素数,第二个是: 循环遍历数组,然后依次调用方法判断
package com.yfs1024.homework;
/**
* Author : Cookie
* Date : 2023/2/15 20:46
* Explain :
* 找素数
*/
public class JudgeSuShu {
public static void main(String[] args) {
judgeNumber(10,100);
}
// 用来判断是不是素数
public static boolean judgeSuShu(int num) {
if (num <= 0) {
System.out.println("请输入大于2的数");
return false;
}
// 这里最小因子就是2,所以没有必要因子比一半大,所以除以2
for (int i = 2; i <= num / 2; i++) {
// 里面的for用来遍历所有的因子
if (num % i == 0) {
return false;
}
}
return true;
}
// 循环中调用并判断素数
public static void judgeNumber(int origin,int bound) {
for (int i = origin; i <= bound; i++) {
if (judgeSuShu(i)) {
System.out.println(i + "是素数");
}
}
}
}
练习6: 数字加密
需求:
某系统的数字密码是一个四位数,如1983,为了安全,需要加密后再传输,加密规则是:对密码中的每位数,都加5 ,再对10求余,最后将所有数字顺序反转,得到一串加密后的新数,请设计出满足本需求的加密程序!
分析: 方法的返回值为加密后的数据, 参数为加密之前的数据
package com.yfs1024.homework;
/**
* Author : Cookie
* Date : 2023/2/15 20:54
* Explain :
*/
public class Encryption {
public static void main(String[] args) {
System.out.println(getEncryption(1234));
}
public static String getEncryption(int num) {
int ge = num % 10;
int shi = num / 10 % 10;
int bai = num / 100 % 10;
int qian = num / 1000 % 10;
// 定义一个数据
int[] arr = {qian, bai, shi, ge};
String str = "";
for (int i = arr.length - 1; i >= 0; i--) {
str += (arr[i] = (arr[i] + 5) % 10);
}
return str;
}
}
练习7: 评委打分案例
需求:
在唱歌比赛中,可能有多名评委要给选手打分,分数是[0 - 100]之间的整数。选手最后得分为:去掉最高分、最低分后剩余分数的平均分,请编写程序能够录入多名评委的分数,并算出选手的最终得分。
分析: 用一个方法来求出数组中的去掉最高分和最低分的平均值. 另一个方法用来输入分数
package com.yfs1024.homework;
import java.util.Scanner;
/**
* Author : Cookie
* Date : 2023/2/15 20:57
* Explain :
*/
public class Calculation {
public static void main(String[] args) {
double avg = getAvg(5);
System.out.println("平均分为:" + avg);
}
public static double[] getScore(int persons) {
Scanner sc = new Scanner(System.in);
double[] arr = new double[persons];
for (int i = 0; i < persons; i++) {
System.out.println("您是第" + (i + 1) + "个评委请输入您评分:");
double score = sc.nextDouble();
if (score < 0 || score > 100) {
System.out.println("您的分数非法,请重新输入:");
i--;
continue;
}
arr[i] = score;
}
return arr;
}
// 该方法用来按照要求,并求出平均值
public static double getAvg(int persons) {
// 得到打分的列表
double[] score = getScore(persons);
double max = score[0];
double min = score[0];
double sum = 0;
for (int i = 0; i < score.length; i++) {
sum += score[i];
if (max < score[i]) {
max = score[i];
} else {
min = score[i];
}
}
double avg = (sum - max - min) / (persons - 2);
return avg;
}
}
练习8:模拟双色球:业务分析、用户投注一组号码
需求: 投注号码由6个红色球号码和1个蓝色球号码组成。红色球号码从1-33中选择;蓝色球号码从1-16中选择

分析: 这里需要设计三个方法: 第一个方法是用户自己猜的双色球的方法,保存成数组并返回,不需要参数. 另一个为系统自动生成的中奖双色球, 最后一个方法为将自己猜的中奖号码和系统生成的方法比较来判定中奖的等级和中奖的奖金
// 先编写录入自己猜的中奖号码,因为红色的号码不能重复,所以需要判断数组中是否有当前的号码,因为下面的方法也需要判断,所以在这里先定义一个方法,判断是否已经存在当前号码
// 定义一个方法来判断当前索引的值是否为数组中的重复的值
public static boolean isExist(int[] arr, int num) {
// 这里只判断红色球是否相等,索引为1,默认是蓝色球
for (int i = 1; i < arr.length; i++) {
if (arr[i] == num) {
return true;
}
}
return false;
}
创建用户自己所猜的中奖号码:
// 该方法用于输入用户的数据
public static int[] createMyself() {
Scanner sc = new Scanner(System.in);
// 创建一个大小为7的数组,用来存双色球的数据
int[] arr = new int[7];
int index = 1;
while (true) {
System.out.println("请输入您的蓝色球的号码");
int lanNum = sc.nextInt();
// 说明这个号码不合法
if (lanNum < 0 || lanNum > 16) {
System.out.println("号码不合法,请重新输入:");
} else {
// 说明合法
arr[0] = lanNum;
// 存入并退出
break;
}
}
// 下面完成红色球的输入
while (true) {
System.out.println("请输入第" + index + "个红色球的号码");
int hongNum = sc.nextInt();
if (hongNum < 0 || hongNum > 33) {
System.out.println("号码不合法,请重新输入:");
} else {
// 说明合法,合法了 在判断是否已经存在
// 调用上面的方法 如果不存在,可以添加
if (!isExist(arr, hongNum)) {
arr[index] = hongNum;
index++;
} else {
// 如果存在了 做出提示
System.out.println("该号码已经输入过,请重新输入:");
}
}
if (index == 7) {
break;
}
}
return arr;
}
下面的代码是系统生成中奖的号码:
// 创建一个系统自动生成的双色球
public static int[] autoGenerateBall() {
Random random = new Random();
// 创建一个数组为系统生成的双色球号码
int[] arr = new int[7];
// 生成蓝色球
arr[0] = random.nextInt(1, 17);
for (int i = 1; i < arr.length; i++) {
while (true) {
int redNum = random.nextInt(1, 34);
if (isExist(arr, redNum)) {
i--;
} else {
arr[i] = redNum;
break;
}
}
}
return arr;
}
下面的代码用来将用户猜的和系统生成的中奖号码作比较,在通过规则输出中奖情况和中奖金额
// 定义一个方法判断对方的中奖情况
public static String judgmentReward(int[] createMyself, int[] autoGenerateBall) {
// 用来计算红色相同的个数
int hongSum = 0;
// 记录蓝色相同的个数
int lanSum = 0;
if (createMyself[0] == autoGenerateBall[0]) {
lanSum = 1;
}
for (int i = 1; i < createMyself.length; i++) {
for (int j = 1; j < autoGenerateBall.length; j++) {
if (createMyself[i] == autoGenerateBall[j]) {
hongSum++;
break;
}
}
}
System.out.println(hongSum + "," + lanSum);
String str = "";
if (hongSum == 6 && lanSum == 1) {
str = "一等奖----1000万";
} else if (hongSum == 6) {
str = "二等奖----500万";
} else if (hongSum == 5 && lanSum == 1) {
str = "三等奖----3000元";
} else if (hongSum == 5) {
str = "四等奖----200元";
} else if (hongSum == 4 && lanSum == 1) {
str = "四等奖----200元";
} else if (hongSum == 4) {
str = "五等奖----10元";
} else if (hongSum == 3 && lanSum == 1) {
str = "五等奖----10元";
} else if (lanSum == 1) {
str = "六等奖----5元";
} else {
str = "抱歉没中~";
}
return str;
}
最后再main方法中测试:
public static void main(String[] args) {
int[] arr1 = createMyself();
for (int anInt : arr1) {
System.out.print(anInt + "\t");
}
System.out.println("----------");
int[] arr2 = autoGenerateBall();
for (int index : arr2) {
System.out.print(index + "\t");
}
System.out.println(judgmentReward(arr1, arr2));
}

浙公网安备 33010602011771号