六、案例
1.买飞机票
注意:
- 头等舱和经济舱是值的选择,用switch比较好;
- 变量可以用原始的减少冗余
/*
5-10月旺季,头等舱打9折、经济舱打8.5折
11-4月淡季,头等舱打7折、经济舱打6.5折
*/
public class Test1 {
public static void main(String[] args) {
// 计算机票的优惠价格
double price = 1000;
System.out.println(calMoney(before, 8, 2));
}
public static double calMoney (double price, int month, int position) {
// double target = 0; // 这个变量定义多余,可以直接赋值给price
// 判断当前是单机和旺季
if (month >=5 && month <= 10) {
switch (position){
case 1:
price = price * 0.9;
break;
case 2:
price = price * 0.85;
break;
default:
System.out.println("没有你选择的仓位!请重新输入!");
}
}
if (month >= 1 && month <= 4 || month >= 11 && month <= 12) {
switch (position){
case 1:
price = price * 0.7;
break;
case 2:
price = price * 0.65;
break;
default: //对网页来说是多余的
System.out.println("没有你选择的仓位!请重新输入!");
}
}
return price;
}
}
2.验证码
注意:
通过随机数的产生控制验证码位置的类型
public class Test2 {
public static void main(String[] args) {
// 生成指定位数的验证码,每位验证码可以是数字、大写字母、小写字母
System.out.println(creatCode(5));
}
public static String creatCode(int n) {
// 定义code记住产生的每个字符
String code = "";
// 定义for循环,控制产生多少位随机字符
Random r = new Random();
for (int i = 0; i < n; i++) {
// 产生随机数,确定每位生成的是数字、小写字母还是大写字母
// 可以用0表示生成的是数字、1是大写字母、2是小写字母
int type = r.nextInt(3);
switch (type) {
case 0:
// 随机生成一个数字
int temp = r.nextInt(9);
code += temp;
break;
case 1:
// 随机生成一个大写字母A65
char ch1 = (char) (r.nextInt(26) + 65);
code += ch1;
break;
case 2:
// 随机生成一个小写字母a97
char ch2 = (char) (r.nextInt(26) + 97);
code += ch2;
break;
}
}
return code;
}
}
3. 评委打分
public class Test3 {
public static void main(String[] args) {
// 评委打分,计算选手的最终得分。打分为[0, 100],需要去掉一个最高分、一个最低分,求平均分
System.out.println(getAverageScore(6));
}
public static double getAverageScore (int number) {
// 1. 首先定义一个动态初始化数组,记录评委打分
int[] scores = new int[number];
// 2. 循环输入评委打分情况
Scanner sc = new Scanner(System.in);
for (int i = 0; i < scores.length; i++) {
System.out.print("请输入第" + (i + 1) + "个评委的得分:");
scores[i] = sc.nextInt();
}
// 3. 计算评分总和以及其中的最大值和最小值
int sum = 0;
int max = scores[0];
int min = scores[0];
for (int i = 0; i < scores.length; i++) {
sum += scores[i];
// 找最大值
if (scores[i] > max) {
max = scores[i];
}
// 找最小值
if (scores[i] < min) {
min = scores[i];
}
}
// 4、 计算平均分并返回,计算结果是整数,返回值要求是double,所以*1.0
return 1.0 * (sum - max - min) / (number - 2);
}
}
4. 加密数据
public class Test4 {
public static void main(String[] args) {
// 数字加密
System.out.println(encrypt(1983));
}
public static String encrypt (int number) {
// 1. 把密码拆分成一个一个的数字
int[] numbers = split((number));
// 2. 把每个数字+5再%10
for (int i = 0; i < numbers.length; i++) {
numbers[i] = (numbers[i] + 5) % 10;
}
// 3. 将数组进行翻转
reverse(numbers);
// 4. 把数字进行拼接并返回
String code = "";
for (int i = 0; i < numbers.length; i++) {
code += numbers[i];
}
return code;
}
// 翻转数组
public static void reverse(int[] numbers) {
for (int i = 0, j = numbers.length - 1; i < j; i++, j--) {
int temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
// 拆分
public static int[] split(int number) {
int[] numbers = new int[4];
for (int i = 0; i < 4; i++) {
numbers[i] = (number) / (int)Math.pow(10,(4 - i - 1));
System.out.println(numbers[i]);
number = (number) % (int)Math.pow(10,(4 - i - 1));
}
return numbers;
}
}
5. 数组拷贝
public class Test5 {
public static void main(String[] args) {
// 拷贝数组
int[] arr = {1, 2, 3};
int[] copyArr = copy(arr);
System.out.println(arr);
System.out.println(copyArr);
// 打印功能也可以写成一个方法,封装起来
for (int i = 0; i < copyArr.length; i++) {
System.out.println(copyArr[i]);
}
}
public static int[] copy(int[] arr) {
// 1. 定义一个长度与arr相同的动态数组
int[] copyArr = new int[arr.length];
// 2. 循环遍历,将相同位置的值存入新数组
for (int i = 0; i < arr.length; i++) {
copyArr[i] = arr[i];
}
return copyArr;
}
}