# 买飞机票
public class Demo1 { | |
public static void main(String[] args) { | |
double price = planePrice(1000, 8, "经济舱"); | |
System.out.println("优惠价是" + price); | |
} | |
// 买飞机票 | |
public static double planePrice(double price, int month, String type) { | |
if (month >= 5 && month <= 10) { | |
switch (type) { | |
case "头等舱": | |
price *= 0.9; | |
break; | |
case "经济舱": | |
price *= 0.85; | |
break; | |
} | |
} else { | |
switch (type) { | |
case "头等舱": | |
price *= 0.7; | |
break; | |
case "经济舱": | |
price *= 0.65; | |
break; | |
} | |
} | |
return price; | |
} | |
} |
# 开发验证码
import java.util.Random; | |
public class Demo2 { | |
public static void main(String[] args) { | |
System.out.println(createCode(4)); | |
} | |
// 开发验证码 | |
public static String createCode(int num) { | |
Random r = new Random(); | |
String code = ""; | |
for (int i = 0; i < num; i++) { | |
int type = r.nextInt(3); | |
switch (type) { | |
case 0: | |
// 随机数字 | |
code += r.nextInt(10); | |
break; | |
case 1: | |
// 随机大写字母:A (65) Z (65+25) | |
//alt + 回车键:转换 | |
char ch1 = (char) (r.nextInt(26) + 65); | |
code += ch1; | |
break; | |
case 2: | |
// 随机小写字母:a (97) z (65+25) | |
char ch2 = (char) (r.nextInt(26) + 97); | |
code += ch2; | |
break; | |
} | |
} | |
return code; | |
} | |
} |
# 评委打分
import java.util.Scanner; | |
public class Demo3 { | |
public static void main(String[] args) { | |
System.out.println("当前选手的得分是:" + getAverageCode(6)); | |
} | |
// 评委打分 | |
public static double getAverageCode(int number) { | |
int[] scores = new int[number]; | |
Scanner sc = new Scanner(System.in); | |
for (int i = 0; i < scores.length; i++) { | |
System.out.println("请输入第" + (i + 1) + "个评委的分数:"); | |
int score = sc.nextInt(); | |
scores[i] = score; | |
} | |
int sum = 0; | |
int max = scores[0]; | |
int min = scores[0]; | |
for (int i = 0; i < scores.length; i++) { | |
int score = scores[i]; | |
sum += score; | |
if (score > max) { | |
max = score; | |
} | |
if (score < min) { | |
min = score; | |
} | |
} | |
return 1.0 * (sum - max - min) / (number - 2); | |
} | |
} |
# 数字加密
public class Demo4 { | |
public static void main(String[] args) { | |
System.out.println("加密后的数字为:" + encrypt(1983)); | |
} | |
// 数字加密 | |
public static String encrypt(int number) { | |
int[] numbers = split(number); | |
for (int i = 0; i < numbers.length; i++) { | |
numbers[i] = (numbers[i] + 5) % 10; | |
} | |
reverse(numbers); | |
String data = ""; | |
for (int i = 0; i < numbers.length; i++) { | |
data += numbers[i]; | |
} | |
return data; | |
} | |
public static int[] split(int number) { | |
int[] numbers = new int[4]; | |
numbers[0] = number / 1000; | |
numbers[1] = (number / 100) % 10; | |
numbers[2] = (number / 10) % 10; | |
numbers[3] = number % 10; | |
return numbers; | |
} | |
public static void reverse(int[] numbers) { | |
for (int i = 0, j = numbers.length - 1; i < j; i++, j--) { | |
int temp = numbers[j]; | |
numbers[j] = numbers[i]; | |
numbers[i] = temp; | |
} | |
} | |
} |
# 数组拷贝
public class Demo5 { | |
public static void main(String[] args) { | |
int[] arr = {11, 22, 33, 44}; | |
int[] newArr = copy(arr); | |
printArray(newArr); | |
} | |
// 数组拷贝 | |
public static int[] copy(int[] arr) { | |
int[] newArr = new int[arr.length]; | |
for (int i = 0; i < arr.length; i++) { | |
newArr[i] = arr[i]; | |
} | |
return newArr; | |
} | |
// 打印数组 | |
public static void printArray(int[] arr) { | |
System.out.print("["); | |
for (int i = 0; i < arr.length; i++) { | |
System.out.print(i == arr.length - 1 ? arr[i] + "]" : arr[i] + ","); | |
} | |
} | |
} |
# 抢红包
import java.util.Random; | |
import java.util.Scanner; | |
public class Demo6 { | |
public static void main(String[] args) { | |
int[] moneys = {11, 88, 666, 999, 9999}; | |
start(moneys); | |
} | |
// 抢红包 | |
public static void start(int[] moneys) { | |
Scanner sc = new Scanner(System.in); | |
Random r = new Random(); | |
for (int i = 1; i <= 5; i++) { | |
System.out.println("请输入任意内容进行抽奖:"); | |
sc.next(); | |
while (true) { | |
int index = r.nextInt(moneys.length); | |
int money = moneys[index]; | |
if (money != 0) { | |
System.out.println("恭喜你,抽中了" + money + "元"); | |
moneys[index] = 0; | |
break; | |
} | |
} | |
} | |
System.out.println("活动结束~~~"); | |
} | |
} |
# 找素数
public class Demo7 { | |
public static void main(String[] args) { | |
// 方法一 | |
search(101, 200); | |
// 方法二 | |
search2(101, 200); | |
// 方法三 | |
for (int i = 101; i <= 200; i++) { | |
if (check(i)) { | |
System.out.println(i); | |
} | |
} | |
} | |
// 找素数 | |
public static void search(int start, int end) { | |
int count = 0; | |
for (int i = start; i <= end; i++) { | |
boolean flag = true; | |
for (int j = 2; j <= i / 2; j++) { | |
if (i % j == 0) { | |
flag = false; | |
break; | |
} | |
} | |
if (flag) { | |
count++; | |
System.out.println(i); | |
} | |
} | |
System.out.println("当前素数的个数是:" + count); | |
} | |
public static void search2(int start, int end) { | |
int count = 0; | |
OUT: | |
// 为外部循环指定标签 | |
for (int i = start; i <= end; i++) { | |
for (int j = 2; j <= i / 2; j++) { | |
if (i % j == 0) { | |
continue OUT; | |
} | |
} | |
count++; | |
System.out.println(i); | |
} | |
System.out.println("当前素数的个数是:" + count); | |
} | |
public static boolean check(int data) { | |
for (int i = 2; i < data / 2; i++) { | |
if (data % i == 0) { | |
return false; | |
} | |
} | |
return true; | |
} | |
} |
# 打印乘法表
public class Demo8 { | |
public static void main(String[] args) { | |
for (int i = 1; i <= 9; i++) { | |
for (int j = 1; j <= i; j++) { | |
System.out.print(j + "x" + i + "=" + (j * i) + "\t"); | |
} | |
System.out.println(); // 换行 | |
} | |
} | |
} |
# 打印三角形
public class Demo9 { | |
public static void main(String[] args) { | |
int n = 4; | |
for (int i = 1; i <= n; i++) { | |
for (int j = 1; j <= (n - i); j++) { | |
System.out.print(" "); | |
} | |
for (int j = 1; j <= (2 * i - 1); j++) { | |
// System.out.print("*"); | |
System.out.print(j % 2 == 0 ? " " : "*"); | |
} | |
System.out.println(); | |
} | |
} | |
} |
# 模拟双色球
import java.util.Random; | |
import java.util.Scanner; | |
public class Demo10 { | |
public static void main(String[] args) { | |
int[] userNumbers = userSelectNumbers(); | |
System.out.println("用户号码为:"); | |
printArray(userNumbers); | |
int[] luckNumbers = createLuckNumbers(); | |
System.out.println("幸运号码为:"); | |
printArray(luckNumbers); | |
judge(userNumbers, luckNumbers); | |
} | |
public static int[] userSelectNumbers() { | |
int[] numbers = new int[7]; | |
Scanner sc = new Scanner(System.in); | |
for (int i = 0; i < numbers.length - 1; i++) { | |
while (true) { | |
System.out.println("请输入第" + (i + 1) + "红球号码(1-33之间,不能重复):"); | |
int number = sc.nextInt(); | |
if (number < 1 || number > 33) { | |
System.out.println("输入的号码不在1~33之间,请重新输入!"); | |
} else { | |
if (check(numbers, number)) { | |
System.out.println("输入的号码重复,请重新输入!"); | |
} else { | |
numbers[i] = number; | |
break; | |
} | |
} | |
} | |
} | |
while (true) { | |
System.out.println("请输入最后1个蓝球号码(1-16):"); | |
int number = sc.nextInt(); | |
if (number < 1 || number > 16) { | |
System.out.println("输入的号码不在1~16之间,请重新输入!"); | |
} else { | |
numbers[6] = number; | |
break; | |
} | |
} | |
return numbers; | |
} | |
public static int[] createLuckNumbers() { | |
int[] numbers = new int[7]; | |
Random r = new Random(); | |
for (int i = 0; i < numbers.length - 1; i++) { | |
while (true) { | |
int number = r.nextInt(33) + 1; | |
if (!check(numbers, number)) { | |
numbers[i] = number; | |
break; | |
} | |
} | |
} | |
numbers[6] = r.nextInt(16) + 1; | |
return numbers; | |
} | |
public static void judge(int[] userNumbers, int[] luckNumbers) { | |
int redCount = 0; | |
int blueCount = 0; | |
for (int i = 0; i < userNumbers.length - 1; i++) { | |
for (int j = 0; j < luckNumbers.length - 1; j++) { | |
if (userNumbers[i] == luckNumbers[j]) { | |
redCount++; | |
break; | |
} | |
} | |
} | |
blueCount = userNumbers[6] == luckNumbers[6] ? 1 : 0; | |
System.out.println("命中的红球数量是:" + redCount); | |
System.out.println("命中的蓝球数量是:" + 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 < 3 && blueCount == 1) { | |
System.out.println("恭喜你,中奖5元~~~"); | |
} else { | |
System.out.println("感谢参与~~~"); | |
} | |
} | |
public static boolean check(int[] numbers, int number) { | |
for (int i = 0; i < numbers.length; i++) { | |
if (numbers[i] == 0) { | |
break; | |
} | |
if (numbers[i] == number) { | |
return true; | |
} | |
} | |
return false; | |
} | |
public static void printArray(int[] arr) { | |
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(); | |
} | |
} |