前言
以下一部分习题是我在学习中总结的,希望分享给大家一起复习、学习,若有错误,希望各位小伙伴能够指出,非常感谢。
习题一
机票按照淡季,旺季,头等舱,经济舱收费,输入机票原价,月份和头等舱或经济舱,按照以下规则计算机票价格(旺季5~10月,头等舱9折,经济舱8.5折,淡季11 ~4月,头等舱7折,经济舱6.5折)
public class dome1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输出机票的原价"); int ticket = sc.nextInt(); System.out.println("请输入当前月份"); int month = sc.nextInt(); System.out.println("请输入当前购买的舱位0表示头等舱1表示经济舱"); int seat = sc.nextInt(); if (month >= 5 && month <= 10) { ticket = getTicket(ticket, seat, 0.9, 0.85); } else if ((month >= 1 && month <= 4) || (month >= 11 && month <= 12)) { ticket = getTicket(ticket, seat, 0.7, 0.65); } else { System.out.println("输入的月份不合法"); } System.out.println(ticket); } public static int getTicket(int ticket, int seat, double v, double v2) { if (seat == 0) { ticket = (int) (ticket * v); } else if (seat == 1) { ticket = (int) (ticket * v2); } else { System.out.println("没有这个舱位"); } return ticket; } }
习题二
判断100~200之间有多少质数,并输出所有质数
public class dome1 { public static void main(String[] args) { int count = 0; for (int i = 101; i < 200; i += 2) { boolean flag = true; for (int j = 2; j < i; j++) { if (i % j == 0) { flag = false; break; } } if (flag) { System.out.println("当前数字" + i + "是质数"); count++; } } System.out.println(count); } }
习题三
定义一个实现随机产生一个无位的验证码,验证码格式长度为5,前四位大写字母或小写字母,最后一位是数字
public class dome1 { public static void main(String[] args) { char[] chs = new char[52]; for (int i = 0; i < chs.length; i++) { if (i <= 25) { chs[i] = (char) (97 + i); } else { chs[i] = (char) (65 + i - 26); } } String result = ""; Random r = new Random(); for (int i = 0; i < 4; i++) { int randomIndex = r.nextInt(chs.length); result += chs[randomIndex]; } int number = r.nextInt(10); result += number; System.out.println(result); } }
习题四
数组元素的复制,把一个数组中的元素复制到另一个数组中
public class dome1 { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; int[] newArr = new int[arr.length]; for (int i = 0; i < arr.length; i++) { newArr[i] = arr[i]; } for (int i = 0; i < newArr.length; i++) { System.out.print(newArr[i] + " "); } } }
习题五
评委打分
在唱歌比赛中,有6位评委给选手打分,范围1~100,选手的得分为去掉一个最高分和一个最低分剩下四个评委的平均分。
public class dome1 { public static void main(String[] args) { int[] scoreArr = getScores(); for (int i = 0; i < scoreArr.length; i++) { System.out.println(scoreArr[i]); } int max = getMax(scoreArr); int min = getMin(scoreArr); int sum = getSum(scoreArr); int avg = (sum - min - max) / (scoreArr.length - 2); System.out.println("选手的最终得分为" + avg); } public static int getSum(int[] scoreArr) { int sum = 0; for (int i = 0; i < scoreArr.length; i++) { sum += scoreArr[i]; } return sum; } public static int getMin(int[] scoreArr) { int min = scoreArr[0]; for (int i = 1; i < scoreArr.length; i++) { if (min > scoreArr[i]) { min = scoreArr[i]; } } return min; } public static int getMax(int[] scoreArr) { int max = scoreArr[0]; for (int i = 1; i < scoreArr.length; i++) { if (max < scoreArr[i]) { max = scoreArr[i]; } } return max; } public static int[] getScores() { int[] scores = new int[6]; Scanner sc = new Scanner(System.in); for (int i = 0; i < scores.length; ) { System.out.println("请输入评委的评分"); int score = sc.nextInt(); if (score >= 0 && score <= 100) { scores[i++] = score; } else { System.out.println("成绩超出了范围,继续录入,当前的i为:" + i); } } return scores; } }
习题六
数字加密和解密
加密先每位数加5,每位数再对10求余数,最后将所有数反转,得到一串新数。
加密
public class dome1 { public static void main(String[] args) { int[] arr = {1, 9, 8, 3}; for (int i = 0; i < arr.length; i++) { arr[i] += 5; } for (int i = 0; i < arr.length; i++) { arr[i] %= 10; } for (int i = 0, j = arr.length - 1; i < j; j--, i++) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } int number = 0; for (int i = 0; i < arr.length; i++) { number = number * 10 + arr[i]; } System.out.println(number); } }
解密
public class dome1 { public static void main(String[] args) { int[] arr = {8, 3, 4, 6}; for (int i = 0, j = arr.length - 1; i < j; i++, j--) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } for (int i = 0; i < arr.length; i++) { if (arr[i] >= 0 && arr[i] <= 4) { arr[i] += 10; } } for (int i = 0; i < arr.length; i++) { arr[i] -= 5; } int number = 0; for (int i = 0; i < arr.length; i++) { number = number * 10 + arr[i]; } System.out.println(number); } }
习题七
抽奖,现金红包分别是(2,588,888,1000,10000)五个奖金,奖项随机不重复,打印每个奖项
public class dome1 { public static void main(String[] args) { int[] arr = {2, 588, 888, 1000, 10000}; int[] newArr = new int[arr.length]; Random r = new Random(); for (int i = 0; i < arr.length; i++) { int randomIndex = r.nextInt(arr.length); int temp = arr[i]; arr[i] = arr[randomIndex]; arr[randomIndex] = temp; } for (int i = 0; i < newArr.length; i++) { System.out.println(arr[i]); } } }
习题八
中奖
投注6个红色球和1个蓝色球,红色从1~33中选择,蓝色球从1 ~16中选择,不同球对应不同的奖金,如下图所示。
奖等 | 红球 | 篮球 | 中奖说明 | 奖金 |
一等奖 | 6 | 1 | 6+1 | 1000万 |
二等奖 | 6 | 0 | 6+0 | 500万 |
三等奖 | 5 | 1 | 5+1 | 3000元 |
四等奖 | 5 | 0 | 5+0 | 200元 |
4 | 1 | 4+1 | 200元 | |
五等奖 | 4 | 0 | 4+0 | 10 |
3 | 1 | 3+1 | 10 | |
六等奖 | 2 | 1 | 2+1 | 5元 |
1 | 1 | 1+1 | 5元 | |
0 | 1 | 0+1 | 5元 |
public class dome1 { public static void main(String[] args) { //1、生成中奖号码 int[] arr = createNumber(); System.out.println("======="); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println("======="); int[] userInputArr = userInputNumber(); int redCount = 0; int blueCount = 0; for (int i = 0; i < userInputArr.length - 1; i++) { int redNumber = userInputArr[i]; for (int j = 0; j < arr.length - 1; j++) { if (redNumber == arr[j]) { redCount++; break; } } } int blueNumber = userInputArr[userInputArr.length - 1]; if (arr[arr.length - 1] == blueNumber) { blueCount++; } if (redCount == 6 && blueCount == 1) { System.out.println("恭喜你中奖1000万"); } else if (redCount == 6 && blueCount == 0) { System.out.println("恭喜你中奖500万"); } else if (redCount == 5 && blueCount == 1) { System.out.println("恭喜你中奖3000"); } else if ((redCount == 5 && blueCount == 0) || (redCount == 4 && blueCount == 1)) { System.out.println("恭喜你中奖200"); } else if ((redCount == 4 && blueCount == 0) || (redCount == 3 && blueCount == 1)) { System.out.println("恭喜你中奖10"); } else if ((redCount == 2 && blueCount == 1) || (redCount == 1 && blueCount == 1) || (redCount == 0 && blueCount == 1)) { System.out.println("恭喜你中奖5"); } else { System.out.println("谢谢惠顾"); } } public static int[] userInputNumber() { int[] arr = new int[7]; Scanner sc = new Scanner(System.in); for (int i = 0; i < 6; ) { System.out.println("请输入第" + (i + 1) + "个号码"); int redNumber = sc.nextInt(); if (redNumber >= 1 && redNumber <= 33) { boolean flag = contains(arr, redNumber); if (!flag) { arr[i++] = redNumber; } else { System.out.println("当前红球号码已经存在,请重新输入"); } } else { System.out.println("当前红球号码超出范围"); } } System.out.println("请输入篮球号码"); while (true) { int bluenumber = sc.nextInt(); if (bluenumber >= 1 && bluenumber <= 16) { arr[arr.length - 1] = bluenumber; break; } else { System.out.println("当前篮球号码超出范围"); } } return arr; } public static int[] createNumber() { Random r = new Random(); int[] arr = new int[7]; for (int i = 0; i < 6; ) { int rednumber = r.nextInt(33) + 1; boolean flag = contains(arr, rednumber); if (!flag) { arr[i++] = rednumber; } } int bluenumber = r.nextInt(16) + 1; arr[arr.length - 1] = bluenumber; return arr; } public static boolean contains(int[] arr, int number) { for (int i = 0; i < arr.length; i++) { if (arr[i] == number) { return true; } } return false; } }