编程思维和编程能力综合案例
买飞机票
机票价格按照淡季旺季,头等舱经济舱收费,输入机票原价,月份和头等舱或经济舱
-
按照下面的规则计算机票价格:
- 旺季(5~10月)
- 头等舱:9折
- 经济舱:8.5折
- 淡季(11月到来年4月 )
- 头等舱:7折
- 经济舱:6.5折
package com.java.test; import java.util.Scanner; public class Ticket { public static void main(String[] args) { System.out.println("请依次输入机票原价,预定月份(数字),舱位级别(1为头登场,2为经济舱):"); Scanner sc = new Scanner(System.in); double price = sc.nextDouble(); int month = sc.nextInt(); int level = sc.nextInt(); System.out.println("机票价格为:" + discount(price, month, level) + "元"); } public static double discount(double price, int month, int level) { if (5 <= month && month <= 10) { switch (level) { case 1: price *= 0.9; break; case 2: price *= 8.5; break; default: System.out.println(); } } else if (month <= 4 && month <= 12) { switch (level) { case 1: price *= 0.7; break; case 2: price *= 0.65; break; default: System.out.println(); } } else { } return price; } }
知识点:流程控制
- 旺季(5~10月)
找素数
判断 101-200之间有多少个素数,并输出所有素数
-
如果除了1和它本身以外,不能被其他正整数整除,就叫素数
package com.java.test; public class Prime { public static void main(String[] args) { int count = 0; for (int i = 101; i < 201; i++) { // 超过10的素数的个位数只有1,3,7,9 if (i % 10 == 1 || i % 10 == 3 || i % 10 == 7 || i % 10 == 9) { count++; System.out.println(i); } } System.out.println("一共有" + count + "个素数"); } }
知识点:位数值获取
随机指定位验证码
-
定义方法实现随机产生一个指定位数的验证码,,每位可能是数字,大写字母,小写字母
package com.java.test; import java.util.Random; import java.util.Scanner; public class Verification { public static void main(String[] args) { System.out.println("请输入您要生成验证码的位数"); Scanner sc = new Scanner(System.in); do { int a = sc.nextInt(); System.out.println(a + "位验证码:" + verify(a)); System.out.println("继续输入可继续获取,或输入-1退出程序!"); if (a == -1) { break; } } while (true); } public static String verify(int a) { Random r = new Random(); // 生成[1,3]的随机数,1为正整数,2为小写字母,3为大写字母 String code = ""; for (int i = 0; i < a; i++) { int type = r.nextInt(3) + 1; switch (type) { case 1: code += r.nextInt(10); break; case 2: char result1 = (char) (r.nextInt(26) + 97); //ASCII:小写字母十进制[97,122] code += result1; break; case 3: char result2 = (char) (r.nextInt(26) + 65); // ASCII:大写字母十进制[65,90] code += result2; break; } } return code; } }
知识点:数据类型的转换
数组元素的复制
-
把一个数组中的元素复制到另一个新数组中去
package com.java.test; import java.util.Scanner; public class Copy { public static void main(String[] args) { resultCopy(); } public static void resultCopy() { System.out.println("请输入原数组的元素的个数"); Scanner r = new Scanner(System.in); int n = r.nextInt(); int[] arrayA = new int[n]; System.out.println("请输入这" + n + "个数:"); for (int i = 0; i < arrayA.length; i++) { arrayA[i] = r.nextInt(); } System.out.println("原数组arrayA的元素为:"); for (int i = 0; i < arrayA.length; i++) { System.out.print(arrayA[i] + "\t"); } System.out.println(); int[] arrayB = new int[arrayA.length]; for (int i = 0; i < arrayA.length; i++) { arrayB[i] = arrayA[i]; } System.out.println("新数组arrayB的元素为:"); for (int i = 0; i < arrayB.length; i++) { System.out.print(arrayB[i] + "\t"); } int array = 1; System.out.println(); if (arrayB.length == arrayA.length) { for (int i = 0; i < arrayB.length; i++) { if (arrayB[i] == arrayA[i]) { array = 1; } else { array = 0; } } } else { System.out.println("复制失败!"); } switch (array) { case 1: System.out.println("复制成功"); break; case 2: System.out.println("复制失败"); break; } } }
知识点:数组相等的条件:长度相等,元素相同
评委打分
-
在唱歌比赛中,有6名评委给选手打分,分数范围是[0,100]之间的整数,选手最后的得分为:去掉最高分,最低分后的4个评委的平均分,请完成上述过程并计算出选手的得分
package com.java.test; import java.util.Random; public class Score { public static void main(String[] args) { getScore(); } public static void getScore() { Random random = new Random(); int[] scores = new int[6]; for (int i = 0; i < scores.length; i++) { scores[i] = random.nextInt(101); } int minScore = scores[0]; int maxScore = scores[0]; for (int i = 1; i < scores.length; i++) { if (minScore > scores[i]) { minScore = scores[i]; } if (maxScore < scores[i]) { maxScore = scores[i]; } } System.out.println("六位评委所给评分为:"); for (int i = 0; i < scores.length; i++) { System.out.print(scores[i] + "\t"); } System.out.println(); System.out.println("去掉一个最高分:" + maxScore + ";和一个最低分:" + minScore); int sumScore = 0; for (int i = 0; i < scores.length; i++) { sumScore += scores[i]; } int result = (sumScore - maxScore - minScore) / 4; System.out.println("所得最终平均分为:" + result); } }
知识点:数组求最值
数字加密
某系统的数字密码,比如1983,采用加密方式进行传输,规则如下:先得到每位数,然后每位数都加上5,再对10求余,最后将所有数字反转,得到一串新数
编写另一个方法,对所得密码进行解密还原到初始状态的密码
-
数组元素反转:指将数组的首部的元素和尾部的元素依次对应交换
package com.java.test; import java.util.Scanner; public class encryption { public static void main(String[] args) { System.out.println("请输入数字密码的位数:"); Scanner sc = new Scanner(System.in); int bits = sc.nextInt(); System.out.println("请输入" + bits + "位数字密码:"); int password = sc.nextInt(); encrypt(bits, password); } public static void encrypt(int a, int b) { int[] arr = new int[a]; // 先模10再除10的方法,使得处理流程是从低位向高位处理,所得结果自然也就具有了反转效果 for (int i = 0; i < arr.length; i++) { arr[i] = b % 10; b /= 10; } System.out.println("所得每位数从低位到高位依次为:"); // 此时遍历已经反转了元素 for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + "\t"); } System.out.println(); System.out.println("对每位数加5,再对10求余并反转数据所得结果:"); for (int i = 0; i < arr.length; i++) { arr[i] += 5; arr[i] %= 10; } for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + "\t"); } } public static void docrypt() { // 方法类似于encrypt(),不作赘述 } }
疑难点:循环处理获取每位数的值,通过模10除10获取
模拟双色球
投注号码由6个红色球号码和1个蓝色球号码组成,红色球号码从[1,33]中选择;蓝色球号码从[1,16]中选择
中奖号码由6个红球和1个蓝球组成,且6个红球要求不登重复
用户输入一组双色球号码分析
-
判断中奖情况:
级别 红球 蓝球 中奖说明 单注奖金分配 一等奖 6 1 中6+1 1000万 二等奖 6 中6+0 500万 三等奖 5 1 中5+1 3000元 四等奖 5 中5+0 200元 4 1 中4+1 五等奖 4 中4+0 10元 3 1 中3+1 六等奖 2 1 中2+1 5元 1 1 中1+1 1 中0+1 package com.java.test; import java.util.Random; import java.util.Scanner; public class ColorBalls { public static void main(String[] args) { //获取奖券号码 System.out.println("请依次合法输入6个红色球号码:注:号码区间[1,33]:"); Scanner sc = new Scanner(System.in); int[] redBalls = new int[6]; for (int i = 0; i < 6; i++) { redBalls[i] = sc.nextInt(); } System.out.println("请再次输入1个蓝色球号码:注:号码区间[1,16]"); int blueBalls = sc.nextInt(); System.out.print("投注号码:"); for (int i = 0; i < redBalls.length; i++) { System.out.print("红:" + redBalls[i] + "\t"); } System.out.println("蓝:" + blueBalls); //生成中奖号码 Random random = new Random(); int[] targetRed = new int[6]; for (int i = 0; i < targetRed.length; i++) { targetRed[i] = random.nextInt(32) + 1; } int targetBlue = random.nextInt(15) + 1; System.out.print("中奖号码:"); for (int i = 0; i < targetRed.length; i++) { System.out.print("红:" + targetRed[i] + "\t"); } System.out.print("蓝:" + targetBlue); //结果判断 System.out.println(); System.out.print("中奖情况说明:"); int redCount = 0; int blueCount = 0; for (int i = 0; i < targetRed.length; i++) { if (redBalls[i] == targetRed[i]) { redCount++; } } if (blueBalls == targetBlue) { blueCount = 1; } if (redCount == 6) { if (blueCount == 1) { System.out.println("一等奖!中6+1!奖金1000万元!"); } else { System.out.println("二等奖!中6+0!奖金500万元"); } } else if (redCount == 5) { if (blueCount == 1) { System.out.println("三等奖!中5+1!奖金3000元!"); } else { System.out.println("四等奖!中5+0!奖金200元"); } } else if (redCount == 4) { if (blueCount == 1) { System.out.println("四等奖!中4+1!奖金200元!"); } else { System.out.println("五等奖!中4+0!奖金10元!"); } } else if (redCount == 3) { if (blueCount == 1) { System.out.println("五等奖!中3+1!奖金10元!"); } } else if (redCount == 2) { if (blueCount == 1) { System.out.println("六等奖!中2+1!奖金5元!"); } else { System.out.println("未中奖!"); } } else { System.out.println("未中奖!"); } } }
知识点:数组相关