第一题:
用for语句嵌套,实现九九乘法表的打印
代码如下:
public class t10 {
public static void main(String[] args) {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + " * " + i + " = " + (i * j) + "\t");
}
System.out.println();
}
}
}
运行结果如下:
1 * 1 = 1
1 * 2 = 2 2 * 2 = 4
1 * 3 = 3 2 * 3 = 6 3 * 3 = 9
1 * 4 = 4 2 * 4 = 8 3 * 4 = 12 4 * 4 = 16
1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 5 * 5 = 25
1 * 6 = 6 2 * 6 = 12 3 * 6 = 18 4 * 6 = 24 5 * 6 = 30 6 * 6 = 36
1 * 7 = 7 2 * 7 = 14 3 * 7 = 21 4 * 7 = 28 5 * 7 = 35 6 * 7 = 42 7 * 7 = 49
1 * 8 = 8 2 * 8 = 16 3 * 8 = 24 4 * 8 = 32 5 * 8 = 40 6 * 8 = 48 7 * 8 = 56 8 * 8 = 64
1 * 9 = 9 2 * 9 = 18 3 * 9 = 27 4 * 9 = 36 5 * 9 = 45 6 * 9 = 54 7 * 9 = 63 8 * 9 = 72 9 * 9 = 81
第二题:
三色球问题:一个口袋中放有12个球,其中有3个红的,3个白的和6个黑的,问从中任取8个共有多少种不同的颜色搭配?
提问:如果任意拿8个球?
1、3个白球最少会出现几个,最多出现几个?
最少出现: 0 最多出现:3
2、3个红球最少会出现几个,最多出现几个?
最少出现: 0 最多出现:3
3、6个黑球最少会出现几个,最多出现几个?
最少出现: 2 最多出现:6
代码如下:
public class t11 {
public static void main(String[] args) {
int redCount = 3;
int whiteCount = 3;
int blackCount = 6;
int totalCount = redCount + whiteCount + blackCount;
int chooseCount = 8;
int minWhite = Math.max(0, chooseCount - redCount - blackCount);
int maxWhite = Math.min(whiteCount, chooseCount);
int minRed = Math.max(0, chooseCount - whiteCount - blackCount);
int maxRed = Math.min(redCount, chooseCount);
int minBlack = Math.max(0, chooseCount - redCount);
int maxBlack = Math.min(blackCount, chooseCount);
int combinations = calculateCombinations(totalCount, chooseCount);
int colorCombinations = calculateCombinations(redCount, minRed) * calculateCombinations(whiteCount, minWhite) * calculateCombinations(blackCount, minBlack);
System.out.println("总共有 " + combinations + " 种不同的颜色搭配");
System.out.println("3个白球最少会出现 " + minWhite + " 个,最多出现 " + maxWhite + " 个");
System.out.println("3个红球最少会出现 " + minRed + " 个,最多出现 " + maxRed + " 个");
System.out.println("6个黑球最少会出现 " + minBlack + " 个,最多出现 " + maxBlack + " 个");
System.out.println("从中任取8个共有 " + colorCombinations + " 种不同的颜色搭配");
}
private static int calculateCombinations(int n, int r) {
int numerator = factorial(n);
int denominator = factorial(r) * factorial(n - r);
return numerator / denominator;
}
private static int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
}
return n * factorial(n - 1);
}
}
运行结果如下:
总共有 495 种不同的颜色搭配
3个白球最少会出现 0 个,最多出现 3 个
3个红球最少会出现 0 个,最多出现 3 个
6个黑球最少会出现 5 个,最多出现 6 个
从中任取8个共有 6 种不同的颜色搭配
第三题:
编写程序实现“登录注册系统”,编写程序实现简单的登录注册功能。程序包含以下4个功能:
(1)登录功能,用户输入正确的账号密码进行成功;
(2)注册功能,输入用户名和密码进行注册;
(3)查看功能,查看所有的用户名与密码;
(4)退出功能,退出系统。
用户可以输入对应的编号进行相应的功能操作。例如,输入1进入登录功能,输入用户名和密码进行注册。
代码如下:
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class yyt1 {
private static Map<String, String> userMap = new HashMap<>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择功能:");
System.out.println("1. 登录");
System.out.println("2. 注册");
System.out.println("3. 查看用户名和密码");
System.out.println("4. 退出系统");
int choice = scanner.nextInt();
switch (choice) {
case 1:
login(scanner);
break;
case 2:
register(scanner);
break;
case 3:
viewUserInfo();
break;
case 4:
System.out.println("退出系统");
return;
default:
System.out.println("输入有误,请重新输入!");
}
}
}
private static void login(Scanner scanner) {
System.out.println("请输入用户名:");
String username = scanner.next();
System.out.println("请输入密码:");
String password = scanner.next();
if (userMap.containsKey(username) && userMap.get(username).equals(password)) {
System.out.println("登录成功!");
} else {
System.out.println("用户名或密码错误!");
}
}
private static void register(Scanner scanner) {
System.out.println("请输入用户名:");
String username = scanner.next();
System.out.println("请输入密码:");
String password = scanner.next();
if (userMap.containsKey(username)) {
System.out.println("该用户已存在!");
} else {
userMap.put(username, password);
System.out.println("注册成功!");
}
}
private static void viewUserInfo() {
for (Map.Entry<String, String> entry : userMap.entrySet()) {
System.out.println("用户名:" + entry.getKey() + ",密码:" + entry.getValue());
}
}
}
运行效果如下:
请选择功能:
1. 登录
2. 注册
3. 查看用户名和密码
4. 退出系统
2
请输入用户名:
ys
请输入密码:
123
注册成功!
请选择功能:
1. 登录
2. 注册
3. 查看用户名和密码
4. 退出系统
1
请输入用户名:
ys
请输入密码:
123
登录成功!
请选择功能:
1. 登录
2. 注册
3. 查看用户名和密码
4. 退出系统
3
用户名:ys,密码:123
请选择功能:
1. 登录
2. 注册
3. 查看用户名和密码
4. 退出系统
2
请输入用户名:
w
请输入密码:
1213
注册成功!
请选择功能:
1. 登录
2. 注册
3. 查看用户名和密码
4. 退出系统
1
请输入用户名:
qw
请输入密码:
123
用户名或密码错误!
请选择功能:
1. 登录
2. 注册
3. 查看用户名和密码
4. 退出系统
4
退出系统
第四题:
编写程序实现“剪刀石头布”的游戏,相信大家都不陌生,本案例要求编写一个剪刀石头布游戏的程序。程序启动后会随机生成1~3的随机数,分别代表剪刀、石头和布,玩家通过键盘输入剪刀、石头和布与电脑进行5轮的游戏,赢的次数多的一方为赢家。若五局皆为平局,则最终结果判为平局。
代码如下:
import java.util.Scanner;
import java.util.Random;
public class yyt2 {
public static void main(String[] args) {
Random random = new Random();
Scanner scanner = new Scanner(System.in);
int playerScore = 0;
int computerScore = 0;
for (int i = 0; i < 5; i++) {
int playerChoice = getUserChoice(scanner, "请输入您的选择(1-剪刀,2-石头,3-布):");
int computerChoice = random.nextInt(3) + 1;
System.out.println("电脑选择了" + computerChoice);
if (playerChoice == computerChoice) {
System.out.println("平局!");
} else if ((playerChoice == 1 && computerChoice == 3) ||
(playerChoice == 2 && computerChoice == 1) ||
(playerChoice == 3 && computerChoice == 2)) {
System.out.println("恭喜您获胜!");
playerScore++;
} else {
System.out.println("很遗憾,您输了。");
computerScore++;
}
}
System.out.println("游戏结束,最终得分:");
System.out.println("您的得分:" + playerScore);
System.out.println("电脑的得分:" + computerScore);
}
运行效果如下:
请输入您的选择(1-剪刀,2-石头,3-布):
1
电脑选择了3
恭喜您获胜!
请输入您的选择(1-剪刀,2-石头,3-布):2
电脑选择了3
很遗憾,您输了。
请输入您的选择(1-剪刀,2-石头,3-布):3
电脑选择了2
恭喜您获胜!
请输入您的选择(1-剪刀,2-石头,3-布):1
电脑选择了3
恭喜您获胜!
请输入您的选择(1-剪刀,2-石头,3-布):1
电脑选择了3
恭喜您获胜!
游戏结束,最终得分:
您的得分:4
电脑的得分:1
第五题:
编写程序实现“抽取幸运观众”,在一些节目活动中,经常会有抽取幸运观众环节。本案例要求编写程序实现幸运观众的抽取,在指定人群中随机抽取一名幸运观众。
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class yyt3 {
public static void main(String[] args) {
List<String> audienceList = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
System.out.println("请输入参与活动的观众人数:");
int num = scanner.nextInt();
scanner.nextLine();
for (int i = 0; i < num; i++) {
System.out.println("请输入第" + (i + 1) + "位观众的名字:");
String name = scanner.nextLine();
audienceList.add(name);
}
Random random = new Random();
int luckyIndex = random.nextInt(audienceList.size());
System.out.println("恭喜以下观众获得幸运奖品:" + audienceList.get(luckyIndex));
}
}
运行结果如下:
请输入参与活动的观众人数:
3
请输入第1位观众的名字:
q
请输入第2位观众的名字:
w
请输入第3位观众的名字:
e
恭喜以下观众获得幸运奖品:w
后续有作业的话还会更新,谢谢各位!