记录学习!!!
1.买飞机票
package com.itheima;
public class Test1 {
public static void main(String[] args) {
double price = calculate(7, "经济舱", 1000.0);
System.out.println(price);
}
public static double calculate(int month, String type, double price) {
if (month >= 5 && month <= 10) {
switch (type) {
case "头等舱":
price *= 0.9;
break;
case "经济舱":
price *= 0.85;
break;
}
} else {
switch (type) {
case "头等舱":
price *= 0.7;
break;
case "经济舱":
price *= 0.65;
break;
}
}
return price;
}
}
2.验证码
package com.itheima;
import java.util.Random;
public class Test2 {
public static void main(String[] args) {
System.out.println(CreatCode(5));
System.out.println(CreatCode(7));
System.out.println(CreatCode(6));
System.out.println(CreatCode(4));
}
//验证码
public static String CreatCode(int n){
Random r = new Random();
String code = "";
for (int i = 0; i < n; i++) {
//n代表验证码位数
//随机数1代表数字,0代表大写字母,2代表小写字母
int type = r.nextInt(3);
switch(type){
case 1:
code += r.nextInt(10);
break;
case 0://A:65
char ch1 = (char)(r.nextInt(26)+65);
code += ch1;
break;
case 2://a:97
char ch2 = (char)(r.nextInt(26)+65);
code += ch2;
break;
}
}
return code;
}
}