目录
案例一:买飞机票
import java.util.Scanner;
public class PlaneDemo {
public static void main(String[] args) {
//案例一:购买飞机票,计算机票优惠价格
//创建扫描器对象
Scanner sc = new Scanner(System.in);
//键盘录入机票原价
System.out.print("请输入机票原价:");
double price = sc.nextDouble();
//键盘录入仓位类型
System.out.print("请输入仓位类型:");
String classType = sc.next();
//键盘输入月份
System.out.print("请输入月份:");
int month = sc.nextInt();
System.out.println("您的折扣票价为:" + discount(price, month, classType));
}
public static double discount(double price,int month, String classType) {
//旺季
if (month >= 5 && month <= 10) {
switch (classType) {
case "头等舱":
price *= 0.9;
break;
case "经济舱":
price *= 0.85;
break;
default:
System.out.println("您输入的有误");
}
//直接else没有考虑月份是否在1-12
} else {
switch (classType) {
case "头等舱":
price *= 0.7;
break;
case "经济舱":
price *= 0.65;
break;
default:
System.out.println("您输入的有误");
}
}
return price;
}
}
代码优化
import java.util.Scanner;
public class PlaneDemo {
public static void main(String[] args) {
//案例一:购买飞机票,计算机票优惠价格
//创建扫描器对象
Scanner sc = new Scanner(System.in);
//键盘录入机票原价
System.out.print("请输入机票原价:");
double price = sc.nextDouble();
//键盘录入仓位类型
System.out.print("请输入仓位类型:");
String classType = sc.next();
//键盘输入月份
System.out.print("请输入月份:");
int month = sc.nextInt();
System.out.println("您的折扣票价为:" + discount(price, month, classType));
}
public static double discount(double price,int month, String classType) {
//旺季
if (month >= 5 && month <= 10) {
switch (classType) {
case "头等舱":
price *= 0.9;
break;
case "经济舱":
price *= 0.85;
break;
default:
System.out.println("您输入的有误");
price = -1;
}
//优化1、考虑月份不为1-12情况
} else if (month == 11 || month == 12 || month>=1 && month<=4){
switch (classType) {
case "头等舱":
price *= 0.7;
break;
case "经济舱":
price *= 0.65;
break;
default:
System.out.println("您输入的有误");
//优化2、提示输入有误显示
price = -1;
}
}
else {
System.out.println("您输入的月份有误");
price = -1;
}
return price;
}
}
总结
- 当判断值匹配的时候使用switch分支结构实现
- 遇到判断区间范围时使用if分支结构实现
案例二、求区间之内的素数
import java.util.Scanner;
public class PrimeDemo {
public static void main(String[] args) {
//求区间内有多少素数
Scanner sc = new Scanner(System.in);
System.out.println("请输入区间:");
int a = sc.nextInt();
int b = sc.nextInt();
System.out.print("区间" + a + "~" + b + "之间的素数有:");
prime(a, b);
}
public static void prime(int a, int b) {
b = max(a, b);
for (int i = a; i <= b; i++) {
//当作信号位
boolean flag = true;
for (int j = 2; j < i / 2; j++) {
if (i % j == 0) {
//System.out.println(i); 错误点:不能在循环中直接打印,会将一个结果打印多次
flag = false;
break;
}
}
if (flag) {
System.out.print(i + " ");
}
}
}
public static int max(int a, int b) {
int max = b;
if (a > b) {
int temp = a;
a = b;
b = temp;
}
return max;
}
}
以上代码中存在了一个bug,当区间内有4时,程序会打印出来4也是素数。
原因:当i=4时,内循环条件为:j=2;j<2循环矛盾,不会继续向下执行,所以信号位的值仍是true,为了消除bug,将内循环条件改为 j=2;j<=i/2。
要打印循环内的值,为了防止对值重复输出可以通过信号位,借助信号位输出需要的内容
案例三、开发验证码
随机产生一个五位验证码。
import java.util.Random;
public class VeriCode {
public static void main(String[] args) {
//随机生成五位验证码
String a = code(5);
System.out.println(a);
}
//1、定义一个方法返回验证码
public static String code(int n) {
Random r = new Random();
// 3、定义一个字符串接收随机字符
String str = "";
// 2、定义一个for循环循环n次
for (int i = 0; i < n; i++) {
//生成一个随机字符,大写字母、小写字母、数字 通过标号0 1 2,来随机生成这三个数
int type = r.nextInt(3); //随机生成 0 1 2
switch (type) {
case 0:
//生成大写字母
char ch = (char)(r.nextInt(26) + 65);
str += ch;
break;
case 1:
//生成小写字母
char ch1 = (char)(r.nextInt(26) +97);
str += ch1;
break;
case 2:
//生成数字
int num = r.nextInt(10);
str += num;
break;
}
}
return str;
}
}
随机验证码的核心逻辑
- 定义一个空字符串用于存放验证码
- 定义一个for循环,对验证码位数进行循环
- 通过生成0、1、2随机数来确定随机生成 小写字母、大写字母和数字
- 将生成的随机数传给switch来随机生成一个小写、大写或者数字并拼接生成的字符串
- 返回拼接完成的字符串
案例四、评委打分
import java.util.Scanner;
public class Mark {
public static void main(String[] args) {
//评委打分
Scanner sc = new Scanner(System.in);
int[] arr1 = new int[6];
System.out.println("请输入6名评委的分数");
for (int i = 0; i < arr1.length; i++) {
arr1[i] = sc.nextInt();
}
double c = score(arr1);
System.out.println(c);
}
public static double score(int[] arr) {
double sum = 0;
int max = arr[0];
int min = arr[0];
for (int i = 0; i < arr.length; i++) {
//找出最高分
if (max < arr[i]) {
max = arr[i];
}
//找出最低分
if (min > arr[i]) {
min = arr[i];
}
sum += arr[i];
}
return (sum - max - min) / (arr.length - 2);
}
}
案例五、数字加密
对密码每个元素进行加五,除10取余再反转,它不仅是加密程序,同时也可以是解密程序
import java.util.Scanner;
public class CodedLock {
public static void main(String[] args) {
//密码加密
Scanner sc = new Scanner(System.in);
//由用户自定义加密位数
System.out.print("请您输入需要加密的位数:");
int length = sc.nextInt();
int[] a = new int[length];
for (int i = 0; i < a.length; i++) {
a[i] = sc.nextInt();
}
System.out.print("加密后的密码为:");
lock(a);
}
public static void lock(int[] arr) {
for (int i = 0; i < arr.length; i++) {
arr[i] = (arr[i] + 5 ) % 10;
}
//数组元素反转
for (int i = 0; i < arr.length / 2; i++) {
int temp = arr[i];
arr[i] = arr[arr.length - i - 1];
arr[arr.length-1-i] = temp;
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
}
}
}
案例六、模拟双色球系统
import java.util.Random;
import java.util.Scanner;
public class DoubleColor {
public static void main(String[] args) {
//模拟双色球
//随机生成中奖号码
System.out.print("中奖号码为:");
int[] luckNum = createLuckNum();
for (int i = 0; i < luckNum.length; i++) {
System.out.print(luckNum[i] + " ");
}
System.out.println();
//用户输入一组号码
int[] userNum = select();
//遍历用户的双色球号码
System.out.print("您的双色球号码为:");
for (int i = 0; i < userNum.length; i++) {
System.out.print(userNum[i] + " ");
}
System.out.println();
//判断中奖情况
System.out.print("您的中奖情况为:");
String result = judge(userNum, luckNum);
System.out.println(result);
}
//随机生成中奖号码
public static int[] createLuckNum() {
int[] arr = new int[7];
int[] red = new int[6];
int blue;
Random r = new Random();
//六个红色 + 一个蓝色
for (int i = 0; i < red.length; i++) {
while (true) {
//先将生成的随机数暂存在data中
int data = r.nextInt(33) + 1;
//定义一个信号位,用来查看随机数是否重复,默认不重复
boolean flag = true;
//定义for循环判断随机数是否重复,每生成一个随机数就往前遍历查看
for (int j = 0; j < i; j++) {
if (red[j] == data) {
//当前数据出现过
flag = false;
break;
}
}
if (flag) {
red[i] = data;
break;
}
}
}
/*for (int i = 0; i < red.length; i++) {
red[i] = r.nextInt(33) + 1;
}
//防止产生相同的随机数
for (int i = 0; i < red.length; i++) {
for (int i1 = 0; i1 < red.length; i1++) {
if (red[i] == red[i1]) {
red[i] = r.nextInt(33) + 1;
}
}
}*/
blue = r.nextInt(16) + 1;
for (int i = 0; i < arr.length; i++) {
if (i != arr.length - 1) {
arr[i] = red[i];
} else {
arr[arr.length - 1] = blue;
}
}
return arr;
}
//用户输入选号
public static int[] select() {
Scanner sc = new Scanner(System.in);
int[] num = new int[7];
for (int i = 0; i < 6; i++) {
System.out.print("请输入第" + (i + 1) + "个红球号码:");
num[i] = sc.nextInt();
}
System.out.print("请输入蓝球号码:");
num[6] = sc.nextInt();
return num;
}
//判断是否中奖
public static String judge(int[] a, int[] b) {
//中的红球数
int red = 0;
//中的蓝球数
int blue = 0;
//中奖情况
String result = "";
//判断中的红球数
for (int i = 0; i < b.length - 1; i++) {
if (a[i] == b[i]) {
red++;
}
}
//判断蓝色球是否中
if (a[a.length - 1] == b[b.length - 1]) {
blue = 1;
}
switch (red) {
case 0:
if (blue == 1) {
result = "恭喜您中了六等奖";
}
case 1:
if (blue == 1) {
result = "恭喜您中了六等奖";
} else {
result = "很遗憾,您没有中奖!!";
}
break;
case 2:
if (blue == 1) {
result = "恭喜您中了五等奖";
}
break;
case 3:
if (blue == 1) {
result = "恭喜您中了五等奖";
}
break;
case 4:
if (blue == 1) {
result = "恭喜您中了六等奖";
} else {
result = "恭喜您中了四等奖";
}
break;
case 5:
if (blue == 1) {
result = "恭喜您中了三等奖";
} else {
result = "恭喜您中了四等奖";
}
break;
case 6:
if (blue == 1) {
result = "恭喜您中了一等奖";
} else {
result = "恭喜您中了二等奖";
}
break;
default:
result = "很遗憾,您没有中奖!!";
}
return result;
}
}