用java设计一个纸牌程序。
答案:3 悬赏:50 手机版
解决时间 2021-11-24 23:01
- 提问者网友:愿为果
- 2021-11-24 07:47
用java设计一个纸牌程序。
最佳答案
- 五星知识达人网友:一把行者刀
- 2021-11-24 08:16
用一个list放这52个元素,每个人随机一个数作为list下标发牌,发掉的牌就从list中remove掉
小写了个来耍:
public static void main(String[] args) {
ArrayList list = new ArrayList();
for (int i = 1; i <= 13; i++) {//生成牌堆
list.add("红心" + i);
list.add("黑桃" + i);
list.add("梅花" + i);
list.add("方块" + i);
}
ArrayList lista = new ArrayList();//生成牌友
ArrayList listb = new ArrayList();
ArrayList listc = new ArrayList();
ArrayList listd = new ArrayList();
while (list.size() > 1) {
lista.add(find(list));
listb.add(find(list));
listc.add(find(list));
listd.add(find(list));
}
System.out.println("甲:" + lista.toString());
System.out.println("乙:" + listb.toString());
System.out.println("丙:" + listc.toString());
System.out.println("丁:" + listd.toString());
}
public static String find(ArrayList list) {
Random random = new Random();
int m = list.size() - 1;//随机数范围为0到剩余牌数量-1
if (m == 0)//对最后一张牌处理
return list.get(0);
int index = Math.abs(random.nextInt()) % m;
String str = list.get(index);
list.remove(index);
return str;
}
小写了个来耍:
public static void main(String[] args) {
ArrayList
for (int i = 1; i <= 13; i++) {//生成牌堆
list.add("红心" + i);
list.add("黑桃" + i);
list.add("梅花" + i);
list.add("方块" + i);
}
ArrayList
ArrayList
ArrayList
ArrayList
while (list.size() > 1) {
lista.add(find(list));
listb.add(find(list));
listc.add(find(list));
listd.add(find(list));
}
System.out.println("甲:" + lista.toString());
System.out.println("乙:" + listb.toString());
System.out.println("丙:" + listc.toString());
System.out.println("丁:" + listd.toString());
}
public static String find(ArrayList
Random random = new Random();
int m = list.size() - 1;//随机数范围为0到剩余牌数量-1
if (m == 0)//对最后一张牌处理
return list.get(0);
int index = Math.abs(random.nextInt()) % m;
String str = list.get(index);
list.remove(index);
return str;
}
全部回答
- 1楼网友:何以畏孤独
- 2021-11-24 10:40
import java.util.ArrayList;
import java.util.Random;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList list = new ArrayList();
String[] str1 = {"桃心","梅花","方片","黑桃"};
String[] str2 = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
for (int i = 0; i < str1.length; i++) {
for (int j = 0; j < str2.length; j++) {
list.add(str1[i]+str2[j]);
}
}
Random random = new Random();
int count = 1;
String r1 ="";
String r2 ="";
String r3 ="";
String r4 ="";
while(list.size() > 0){
int j = random.nextInt(list.size());
if(count == 1){
r1 += list.remove(j) + " ";
}
if(count == 2){
r2 += list.remove(j) + " ";
}
if(count == 3){
r3 += list.remove(j) + " ";
}
if(count == 4){
r4 += list.remove(j) + " ";
count = 0;
}
count ++;
}
System.out.println(r1);
System.out.println(r2);
System.out.println(r3);
System.out.println(r4);
}
}
import java.util.Random;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList
String[] str1 = {"桃心","梅花","方片","黑桃"};
String[] str2 = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
for (int i = 0; i < str1.length; i++) {
for (int j = 0; j < str2.length; j++) {
list.add(str1[i]+str2[j]);
}
}
Random random = new Random();
int count = 1;
String r1 ="";
String r2 ="";
String r3 ="";
String r4 ="";
while(list.size() > 0){
int j = random.nextInt(list.size());
if(count == 1){
r1 += list.remove(j) + " ";
}
if(count == 2){
r2 += list.remove(j) + " ";
}
if(count == 3){
r3 += list.remove(j) + " ";
}
if(count == 4){
r4 += list.remove(j) + " ";
count = 0;
}
count ++;
}
System.out.println(r1);
System.out.println(r2);
System.out.println(r3);
System.out.println(r4);
}
}
- 2楼网友:轻雾山林
- 2021-11-24 09:15
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
public class Test3 {
public static String[] str = { "2", "3", "4", "5", "6", "7", "8", "9",
"10", "J", "Q", "K", "A" };
public static String[] color = { "红", "黑", "梅", "方" };
private static List list=new ArrayList();
public Test3(){
for(int i=0;i for(int j=0;j list.add(str[i]+color[j]);
}
}
}
public static void main(String[] args) {
Test3 t = new Test3();
for(int i=1;i<4;i++){
Set set = new HashSet();
StringBuffer sBuffer = new StringBuffer();
while(true){
int m = (int)(Math.random()*(52-(i-1)*13));
set.add(list.get(m));
if(set.size()>12){
break;
}
}
Iterator iter = set.iterator();
while(iter.hasNext()){
sBuffer.append(iter.next()+",");
}
list.removeAll(set);
System.out.println("第"+i+"个人的手牌:"+sBuffer.toString());
}
System.out.print("第4个人的手牌:");
for(String s:list){
System.out.print(s+",");
}
}
}
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
public class Test3 {
public static String[] str = { "2", "3", "4", "5", "6", "7", "8", "9",
"10", "J", "Q", "K", "A" };
public static String[] color = { "红", "黑", "梅", "方" };
private static List
public Test3(){
for(int i=0;i
}
}
}
public static void main(String[] args) {
Test3 t = new Test3();
for(int i=1;i<4;i++){
Set
StringBuffer sBuffer = new StringBuffer();
while(true){
int m = (int)(Math.random()*(52-(i-1)*13));
set.add(list.get(m));
if(set.size()>12){
break;
}
}
Iterator iter = set.iterator();
while(iter.hasNext()){
sBuffer.append(iter.next()+",");
}
list.removeAll(set);
System.out.println("第"+i+"个人的手牌:"+sBuffer.toString());
}
System.out.print("第4个人的手牌:");
for(String s:list){
System.out.print(s+",");
}
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯