自己做的方法
写一个循环,生成一副扑克
public class Poker {
String suit;// 花色
String congt;// 点数
public void giveMe(int i,int j,int k) {
String hs[]= {"红桃","黑桃","方片","草花"};
String ds[]= {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
// 写一个循环,生成一副扑克
String pokers[]=new String[52];
// 遍历数组,取到hs[]和ds[]的所有组合,给Poker对象赋值,然后存入pokers[]
pokers[i]=hs[j]+ds[k];
System.out.print(pokers[i]+" ");
// +","+i+","+j+","+k(测试用)
if(k==12) {System.out.println("");}
}
public static void main(String[] args) {
Poker cd=new Poker();
int j =0;
int k =0;
for(int i = 0;i<52;i++) {
if((i+1)%13==0 && i!=0) {
if(i==51) {cd.giveMe(i,j,k);return;}
j++;
cd.giveMe(i,j,k);
k=0;
continue;
}
cd.giveMe(i,j,k);
k++;
}
}
}
老师做的方法
public class Poker {
String suit;// 花色
String count;// 点数
public void giveMe() {
String hs[] = {"红桃","黑桃","方片","草花"};
String ds[] = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
// 写一个循环,生成一副扑克
Poker pokers[] = new Poker[52];
int index = 0;
// 遍历数组,取到hs[]和ds[]的所有组合,给Poker对象赋值,然后存入pokers[]
for(int i = 0;i < hs.length;i++) {
for(int j = 0;j < ds.length;j++) {
pokers[index] = new Poker();
pokers[index].suit = hs[i];
pokers[index].count = ds[j];
index++;
}
}
for(Poker p : pokers) {
System.out.println(p.suit+p.count);
}
}
public static void main(String[] args) {
Poker p = new Poker();
p.giveMe();
}
}
总结老师的之后用创建对象的方法做的
package com.neuedu.HomeWork_0306;
public class Poker {
String suit;// 花色
String congt;// 点数
public void giveMe() {
Poker pokers[]=new Poker[52];
// 写一个循环,生成一副扑克
String hs[]= {"红桃","黑桃","方片","草花"};
String ds[]= {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
// 遍历数组,取到hs[]和ds[]的所有组合,给Poker对象赋值,然后存入pokers[]
// System.out.print(pokers[i].suit+pokers[i].congt+" ");
// +","+i+","+j+","+k(测试用)
int j =0;
int k =0;
for(int i = 0;i<52;i++) {
if((i+1)%13==0 && i!=0) {
if(i==51) {
pokers[i]=new Poker();
pokers[i].suit=hs[j];
pokers[i].congt=ds[k];continue;}
System.out.println(i+","+j+","+k);
pokers[i]=new Poker();
pokers[i].suit=hs[j];
pokers[i].congt=ds[k];
j++;
k=0;
continue;
}
pokers[i]=new Poker();
pokers[i].suit=hs[j];
pokers[i].congt=ds[k];
k++;
}
int l =1;
for(int i = 0;i<pokers.length;i++) {
System.out.print(pokers[i].suit+pokers[i].congt);
if(l%13==0) {
System.out.println("");
l=1;
continue;
}
l++;
}
}
public static void main(String[] args) {
Poker cd=new Poker();
cd.giveMe();
}
}