几个语法上容易出现的错误:
1:定义内部类Poke类并在主函数中调用时,如果采用以下方法会产生报错(若是外部类则不会出现这种错误):
解决方法:
注:Class_poke是最大的类的名称
2:主函数要加static否则无法以Java程序的形式运行
3:在进行花色比较的时候String类型无法直接比较大小,因此需要采用String.compareTo函数
4:Java中括号中一定是真值表达式即bool值
import java.util.Random;
public class Class_poke {
class Poke{
int name;
String color;
public void setPoke(int n){
int Color=n%4;
int Name=n/4;
name=Name+2;
switch(Color) {
case 0:color="方片♦";break;
case 1:color="梅花♣";break;
case 2:color="黑桃♠";break;
case 3:color="红桃♥";break;
}
}
}
public static void main(String[] args) {
Class_poke p=new Class_poke();
Poke pok[]=new Poke[52];
for(int i=0;i<52;i++) {
pok[i]=p.new Poke();
}
int poke[]=new int[52];
for(int i=0;i<52;i++) {
poke[i]=1;
}
Random random=new Random();
for(int i=0;i<52;i++) {
int j=0;
do {
j=random.nextInt(52);
if(poke[j]==1)break;
}while(true);
Poke po=p.new Poke();
po.setPoke(j);
poke[j]--;
pok[i].color=po.color;
pok[i].name=po.name;
}
for(int i=0;i<4;i++) {
for(int j=0+13*i;j<13+13*i;j++) {
for(int k=0+13*i;k<13+13*i-1;k++) {
if(pok[k].name>pok[k+1].name) {
int name;String color;
name=pok[k].name;
color=pok[k].color;
pok[k].name=pok[k+1].name;
pok[k].color=pok[k+1].color;
pok[k+1].name=name;
pok[k+1].color=color;
}
else if(pok[k].name==pok[k+1].name&&pok[k].color.compareTo(pok[k+1].color)>0) {
int name;String color;
name=pok[k].name;
color=pok[k].color;
pok[k].name=pok[k+1].name;
pok[k].color=pok[k+1].color;
pok[k+1].name=name;
pok[k+1].color=color;
}
}
}
}
for(int i=0;i<52;i++) {
if(i%13==0)System.out.print("Player"+(i/13+1)+": ");
System.out.print(pok[i].color+":");
switch(pok[i].name) {
case 11:System.out.print("J ");break;
case 12:System.out.print("Q ");break;
case 13:System.out.print("K ");break;
case 14:System.out.print("A ");break;
default:System.out.print(pok[i].name+" ");break;
}
if((i+1)%13==0)System.out.println();
}
return;
}
}