练习一:飞机票
需求:
机票价格按照淡季旺季、头等舱和经济舱收费、输入机票原价、月份和头等舱或经济舱。
按照如下规则计算机票价格:旺季(5-10月)头等舱9折,经济舱8.5折,淡季(11月到来年4月)头等舱7折,经济舱6.5折。
代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 package com.itheima.test;import java.util.Scanner;public class Test1 { 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; } }
练习二:打印素数
判断101~200之间有多少个素数,并输出所有素数。
备注:素数就是质数
代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 package com.itheima.test;public class Test2 { 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.println("当前数字" +i+"是质数" ); count++; } } System.out.println("一共有" + count + "个质数" ); } }
练习三:验证码
需求:
定义方法实现随机产生一个5位的验证码
验证码格式:
长度为5
前四位是大写字母或者小写字母
最后一位是数字
代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 package com.itheima.test;import java.util.Random;public class Test3 { 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 = result + chs[randomIndex]; } int number = r.nextInt(10 ); result = result + number; System.out.println(result); } }
练习四:复制数组
需求:
把一个数组中的元素复制到另一个新数组中去。
代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 package com.itheima.test;public class Test4 { 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.println(newArr[i]); } } }
练习五:评委打分
需求 :
在唱歌比赛中,有6名评委给选手打分,分数范围是[0 - 100]之间的整数。选手的最后得分为:去掉最高分、最低分后的4个评委的平均分,请完成上述过程并计算出选手的得分。
代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 package com.itheima.test;import java.util.Scanner;public class Test5 { 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 - max - min)/(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 = sum + scoreArr[i]; } return sum; } public static int getMax (int [] scoreArr) { int max = scoreArr[0 ]; for (int i = 1 ; i < scoreArr.length; i++) { if (scoreArr[i] > max){ max = scoreArr[i]; } } return max; } public static int getMin (int [] scoreArr) { int min = scoreArr[0 ]; for (int i = 1 ; i < scoreArr.length; i++) { if (scoreArr[i] < min){ min = scoreArr[i]; } } return min; } 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; i++; }else { System.out.println("成绩超出了范围,继续录入,当前的i为:" + i); } } return scores; } }
练习六:数字加密
需求:
某系统的数字密码(大于0),比如1983,采用加密方式进行传输。
规则如下:
先得到每位数,然后每位数都加上5 , 再对10求余,最后将所有数字反转,得到一串新数。
举例:
1 2 3 4 5 1 9 8 3 +5 6 14 13 8 %10 6 4 3 8 反转 8 3 4 6 加密后的结果就是:8346
代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 package com.itheima.test;public class Test6 { public static void main (String[] args) { int [] arr = {1 , 9 , 8 , 3 }; for (int i = 0 ; i < arr.length; i++) { arr[i] = arr[i] + 5 ; } for (int i = 0 ; i < arr.length; i++) { arr[i] = arr[i] % 10 ; } for (int i = 0 , j = arr.length - 1 ; i < j; i++, j--) { 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); } }
练习六扩展:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 package com.itheima.test;public class Test7 { public static void main (String[] args) { int number = 12345 ; int temp = number; int count = 0 ; while (number != 0 ){ number = number / 10 ; count++; } int [] arr = new int [count]; int index = arr.length -1 ; while (temp != 0 ){ int ge = temp % 10 ; temp = temp / 10 ; arr[index] = ge; index--; } for (int i = 0 ; i < arr.length; i++) { System.out.print(arr[i] + " " ); } } }
练习七:数字解密
把上一题加密之后的数据进行解密
代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 package com.itheima.test;public class Test8 { 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] = arr[i] + 10 ; } } for (int i = 0 ; i < arr.length; i++) { arr[i] = arr[i] - 5 ; } int number = 0 ; for (int i = 0 ; i < arr.length; i++) { number = number * 10 + arr[i]; } System.out.println(number); } }
练习八:抽奖
需求:
一个大V直播抽奖,奖品是现金红包,分别有{2, 588 , 888, 1000, 10000}五个奖金。请使用代码模拟抽奖,打印出每个奖项,奖项的出现顺序要随机且不重复。打印效果如下:(随机顺序,不一定是下面的顺序)
1 2 3 4 5 888 元的奖金被抽出588 元的奖金被抽出10000 元的奖金被抽出1000 元的奖金被抽出2 元的奖金被抽出
解法一:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 package com.itheima.test;import java.util.Random;public class Test9 { 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 < 5 ; ) { int randomIndex = r.nextInt(arr.length); int prize = arr[randomIndex]; boolean flag = contains(newArr, prize); if (!flag){ newArr[i] = prize; i++; } } for (int i = 0 ; i < newArr.length; i++) { System.out.println(newArr[i]); } } public static boolean contains (int [] arr,int prize) { for (int i = 0 ; i < arr.length; i++) { if (arr[i] == prize){ return true ; } } return false ; } }
解法二:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 package com.itheima.test;import java.util.Random;public class Test10 { public static void main (String[] args) { int [] arr = {2 , 588 , 888 , 1000 , 10000 }; 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 < arr.length; i++) { System.out.println(arr[i]); } } }
练习九:双色球
代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 package com.itheima.test;import java.util.Random;import java.util.Scanner;public class Test11 { public static void main (String[] args) { 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 (blueNumber == arr[arr.length - 1 ]){ 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; i++; } 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() { int [] arr = new int [7 ]; Random r = new Random (); for (int i = 0 ; i < 6 ; ) { int redNumber = r.nextInt(33 ) + 1 ; boolean flag = contains(arr, redNumber); if (!flag) { arr[i] = redNumber; i++; } } 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 ; } }