1.机票的价格跟季节挂钩,按照如下规则计算机票价格
//自动
旺季(5~10月)头等舱9折,经济舱8.5折;淡季(11月到来年4月)头等舱7折,经济舱6.5折。
请设计一个方法用于计算机票的价格。
思考,程序的健壮性:
1.自识别当前时间,判断出月份、旺季、淡季,给出对应的折扣
2.控制台,输入1位头等舱,2位经济舱;再从控制台输入的机票价格当做用户买机票时对应机票的原价
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要购买的舱位:");
System.out.println("1.头等舱");
System.out.println("2.经济舱");
int falge = scanner.nextInt();
System.out.println("请输入选择票价:");
double money = scanner.nextDouble();
double newMoney = jisuan(falge,money);
System.out.println("折扣后票价为:"+newMoney);
}
private static double jisuan(int falge, double money) {
Calendar cal=Calendar.getInstance();//使用日历类
int month=cal.get(Calendar.MONTH)+1;//得到月,因为从0开始的,所以要加1
double zhekou = 0;
if(month == 5 || month == 6||month == 7||month == 8||month == 9||month == 10){
if (falge == 1)
zhekou = 0.9;
else
zhekou = 0.85;
}else{
if (falge == 2)
zhekou = 0.7;
else
zhekou = 0.65;
}
return money*zhekou;
}
}
发现问题:
如何获取当前的时间如:2022-7-9 10:00 ,并单独截取出年、月、日、时间等
2、浏览网站的时候经常要填写表单,其中验证码就是一部分,请定义一个方法实现随机产生一个5位的验证码,要求如下:
验证码格式:1、长度为5;2、前四位是大写字母或者小写字母;3、最后一位是数字
思路:
1.将数字与字母分开生成
2.如何随机生成大小写字母?
cha[] 用来存储52个大小写字母
在随机生成0-51的数组下标index
定义一个长度为4的字符数组str[],循环遍历他,char(index)赋值给str
即实现随机生成大小写字母
3.随机生成0-9的数字
4.将str[]遍历一遍,将每个元素拼接到一个新字符串newStr中
5.将生成的数字转化为String类型与newStr进行拼接
public class Test1 {
public static void main(String[] args) {
String str = yanzhengma();
System.out.println(str);
}
public static String yanzhengma() {
char[] cha = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
char[] str = new char[4];
for (int i = 0; i < str.length; i++) {
str[i] = cha[(int) (Math.random() * cha.length)];
}
String str1 = (int) (Math.random() * 10) + " ";
String str2 = "";
for (char value : str)
str2 = str2 + value;
str2 = str2 + str1;
return str2;
}
}
*发现问题,思考:
1.int类型转String类型的方法,Strin类型转int类型的方法
(1)String.valueOf(i)
(2)Integer.toString(i)
(3)i+“”
2.如何随机获取大小写字母
思路两种,一种为把26个字符存进char数组中,然后生成1-26随机数当做下标来获取
一种为asccl码,即随机数生成对应的asscll码