文章目录
- 综合应用
- 案例:找素数
- 数组元素的复制
- 数字加密
- 模拟双色球
综合应用
涉及的知识点:
- 变量、数组
- 运算符:基本运算符、关系运算符、逻辑运算符
- 流程控制:if、switch、for、while、死循环、循环嵌套
- 跳转关键字:break、continue、return
- 方法
- ……
案例:找素数
需求:判断101-200之间有多少个素数,并输出所有素数
public class Case1 {public static void main(String[] args) {int count = 0;for (int i = 101; i < 200; i += 2) {boolean flag = true;double range = Math.sqrt(i);for (int j = 2; j < range; j++) {if (i % j == 0) {flag = false;}}if (flag) {count++;System.out.print(i + "\t");}}System.out.println("101-200之间一共有" + count + "个素数");}
}
数组元素的复制
把一个数组中的元素复制到另一个新数组中去
public class Case2 {public static void main(String[] args) {int[] arr = {11, 22, 33, 44, 55, 66};int[] arr2 = arrayCopy(arr);printArray(arr);printArray(arr2);}public static void printArray(int[] arr) {if (arr == null || arr.length == 0) {System.out.println("[ ]");return;}System.out.print("[");for (int i = 0; i < arr.length; i++) {System.out.print(i != arr.length - 1 ? arr[i] + ", " : arr[i]);}System.out.println("]");}public static int[] arrayCopy(int[] arr) {if (arr == null) return null;if (arr.length == 0) return new int[0];int[] arrCopy = new int[arr.length];for (int i = 0; i < arr.length; i++) {arrCopy[i] = arr[i];}return arrCopy;}
}
数字加密
某系统的数字密码,比如1983,采用加密方式进行传输,规则如下:先得到每位数,然后每位数都加上5,再对10求余,最后将所有数字反转,得到一串新数。
public class Case3 {public static void main(String[] args) {int[] arr = {8, 4, 7, 9};encryptString(arr); // 加密Case2.printArray(arr);encryptString(arr); // 解密Case2.printArray(arr);}public static void encryptString(int[] plaintext) {for (int i = 0; i < plaintext.length; i++) {plaintext[i] = (plaintext[i] + 5) % 10;}for (int i = 0, j = plaintext.length - 1; i < j; i++, j--) {int temp = plaintext[i];plaintext[i] = plaintext[j];plaintext[j] = temp;}}
}
模拟双色球
规则:
- “双色球”每注投注号码由6个红色球号码和1个蓝色球号码组成。红色球号码从1–33中选择;蓝色球号码从1–16中选择。
import java.util.Random;
import java.util.Scanner;public class Case4 {public static void main(String[] args) {int[] luckNumber = generateNumber();Case2.printArray(luckNumber);int[] userInput = customerInput();check(userInput, luckNumber);}public static int[] generateNumber() {int[] arr = new int[7];Random r = new Random();for (int i = 0; i < arr.length - 1; i++) {// 找到一个和之前不重复的数据while (true) {boolean canUse = true;int data = r.nextInt(33) + 1;for (int i1 = 0; i1 < i; i1++) {if (data == arr[i1]) {canUse = false;break;}}if (canUse) {arr[i] = data;break;}}}// 生成最后一位蓝球arr[arr.length - 1] = r.nextInt(16) + 1;return arr;}public static int[] customerInput() {int[] arr = new int[7];Scanner sc = new Scanner(System.in);System.out.println("请输入6个红球的号码:");for (int i = 0; i < 6; i++) {// 不允许输入重复数字while (true) {boolean canUse = true;int data = sc.nextInt();if (data > 33 || data < 1) {System.out.println("输入了非可选号码...");canUse = false;}for (int i1 = 0; i1 < i; i1++) {if (data == arr[i1]) {System.out.println("输入了重复号码...");canUse = false;break;}}if (canUse) {arr[i] = data;break;}}}System.out.println("请输入1个蓝球的号码:");while (true) {boolean canUse = true;int data = sc.nextInt();if (data > 16 || data < 1) {System.out.println("输入了非可选号码...");canUse = false;}if (canUse) {arr[6] = data;break;}}return arr;}public static void check(int[] userInput, int[] luckNumbers) {int count1 = 0;int count2 = userInput[6] == luckNumbers[6] ? 1 : 0;for (int i = 0; i < userInput.length - 1; i++) {for (int i1 = 0; i1 < luckNumbers.length - 1; i1++) {if (userInput[i] == luckNumbers[i1])count1++;}}// 针对奖项输出switch (count1) {case 0:case 1:if (count2 == 1)System.out.println("恭喜获得六等奖");if (count2 == 0)System.out.println("很遗憾,没有中奖");break;case 2:case 3:if (count2 == 1)System.out.println("恭喜获得五等奖");if (count2 == 0)System.out.println("很遗憾,没有中奖");break;case 4:if (count2 == 0)System.out.println("恭喜获得五等奖");if (count2 == 1)System.out.println("恭喜获得四等奖");break;case 5:if (count2 == 0)System.out.println("恭喜获得四等奖");if (count2 == 1)System.out.println("恭喜获得三等奖");break;case 6:if (count2 == 0)System.out.println("恭喜获得二等奖");if (count2 == 1)System.out.println("恭喜获得一等奖");break;default:System.out.println("很遗憾,没有中奖");}}
}