练习一:飞机票
需求:
机票价格按照淡季旺季、头等舱和经济舱收费、输入机票原价、月份和头等舱或经济舱。
按照如下规则计算机票价格:旺季(5-10月)头等舱9折,经济舱8.5折,淡季(11月到来年4月)头等舱7折,经济舱6.5折。
代码示例:
import java.util.Scanner;
/**
* @ClassName: codePractice01
* @Author: Kox
* @Data: 2023/1/13
* @Sketch:
*/
public class codePractice01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入飞机原价格:");
int price = sc.nextInt();
System.out.print("请输入月份:");
int month = sc.nextInt();
System.out.print("请输入舱位:(0是头等,1是经济)");
int level = sc.nextInt();
if (month >= 5 && month <= 10) {
price = getPrice(price, level, 0.9, 0.85);
} else if ((month >= 1 && month <= 4) || (month >= 11 && month <= 12)) {
price = getPrice(price, level, 0.7, 0.65);
} else {
System.out.println("月份输入错误");
}
System.out.println(price);
}
public static int getPrice(int price, int level , double v0, double v1) {
if (level == 0) {
price = (int) (price * v0);
} else if (level == 1){
price = (int) (price * v1);
} else {
System.out.println("没有这个舱位!");
}
return price;
}
}
练习二:打印素数
判断101~200之间有多少个素数,并输出所有素数。
备注:素数就是质数
代码示例:
/**
* @ClassName: cn.kox.practice.codePractice02
* @Author: Kox
* @Data: 2023/1/13
* @Sketch: 找质数
*/
public class codePractice02 {
public static void main(String[] args) {
int count = 0;
for (int i = 101; i <= 200; i++) {
boolean flag = true;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
flag = false;
break;
}
}
if (flag) {
System.out.print(i + "\t");
count++;
}
}
System.out.println("\n有" + count + "个质数。");
}
}
练习三:验证码
需求:
定义方法实现随机产生一个5位的验证码
验证码格式:
长度为5
前四位是大写字母或者小写字母
最后一位是数字
代码示例:
import java.util.Arrays;
import java.util.Random;
/**
* @ClassName: codePractice03
* @Author: Kox
* @Data: 2023/1/13
* @Sketch: 开发验证码
*/
public class codePractice03 {
public static void main(String[] args) {
char[] letter = new char[52];
for (int i = 0; i < letter.length; i++) {
if (i <= 25) {
letter[i] = (char)(97 + i);
} else {