JAVA语言程序设计(第10版)基础篇第14章第3题

Wenjian96 2017-08-16 07:18:23
(显示三张牌)请写一个程序,显示从一副52张的扑克牌中随机选择的三张牌。牌的图像文件命名为1.png, 2.png,......,52.png,并保存在image/card 目录下。三张牌都是不同的并且是随机选择的。
书本上的提示是:可以这样随机选择牌,想将数字1~52保存在一个数组列表中,按照11.12节中介绍的方法进行一次随机洗牌,然后使用数组列表中前面三个数组作为图像的文件名。
问题:怎么使用数组列表中前面三个数组作为图像的文件名?
或者有没有其它的方法?
...全文
441 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Wenjian96 2017-08-17
  • 打赏
  • 举报
回复
引用 6楼我是你的主体 的回复:
谢谢各位的回答,不过和我想要的答案有点不一样。我是想问怎么点击运行后,会调用对应图片,如图片这样
这一章讲的是JavaFX的知识
Wenjian96 2017-08-17
  • 打赏
  • 举报
回复
谢谢各位的回答,不过和我想要的答案有点不一样。我是想问怎么点击运行后,会调用对应图片,如图片这样
Freefish1994 2017-08-17
  • 打赏
  • 举报
回复
可以利用Set保证随机数不重复

public class Test {

	public static void main(String[] args) {
		Set<Integer> imgIndex = new HashSet<Integer>();
		getImgIndex(3, imgIndex);
		System.out.println("抽取的牌是:");
		for(Integer entity : imgIndex)
			System.out.println(entity + ".png");
	}

	private static void getImgIndex(int n, Set<Integer> imgIndex) {
		for(int i = 0;i < n;i++){
			int temp = new Random().nextInt(52) + 1;
			imgIndex.add(temp);
		}
		if (imgIndex.size() < n)
			getImgIndex(n - imgIndex.size(), imgIndex);
	}
	
}
soton_dolphin 2017-08-17
  • 打赏
  • 举报
回复
引用 2 楼 saerhuashi 的回复:
[quote=引用 1 楼 soton_dolphin 的回复:] int[] imgs= new int[52]; for(int i = 0;i<52;i++){ imgs[i] = i+1; } Random rand = new Random(); for(int i =0; i < 3; i++){ System.out.println("card file name :" + imgs[rand.nextInt(53)]+".png"; }
这个随机是有问题的,可能会取到相同的值 [/quote] 你自己可以根据Fisher–Yates shuffle 写一个程序打乱这个数组的次序
soton_dolphin 2017-08-17
  • 打赏
  • 举报
回复
你自己可以根据Fisher–Yates shuffle 写一个程序打乱这个数组的次序
saerhuashi 2017-08-16
  • 打赏
  • 举报
回复
引用 1 楼 soton_dolphin 的回复:
int[] imgs= new int[52]; for(int i = 0;i<52;i++){ imgs[i] = i+1; } Random rand = new Random(); for(int i =0; i < 3; i++){ System.out.println("card file name :" + imgs[rand.nextInt(53)]+".png"; }
这个随机是有问题的,可能会取到相同的值
soton_dolphin 2017-08-16
  • 打赏
  • 举报
回复
int[] imgs= new int[52]; for(int i = 0;i<52;i++){ imgs[i] = i+1; } Random rand = new Random(); for(int i =0; i < 3; i++){ System.out.println("card file name :" + imgs[rand.nextInt(53)]+".png"; }

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧