问题:能设字符数组,字符串数组吗?
import java.math.*;
public class Card{
String[] cardSuit=new String[4];
cardSuit[0]= "方块 ";
cardSuit[1]= "梅花 ";
cardSuit[2]= "红桃 ";
cardSuit[3]= "黑桃 ";
String[] cardNum=new String[13];
cardNum[0]= "A ";
for(int i=1;i <10;i++){
cardNum[i]=(String)(i+1);
}
cardNum[10]= "J ";
cardNum[11]= "Q ";
cardNum[12]= "K ";
public static void main(String[] args){
String a1,a2,a3;
a1=cardSuit[(int)(Math.random()*4)]+cardNum[(int)(Math.random()*13)];
a2=cardSuit[(int)(Math.random()*4)]+cardNum[(int)(Math.random()*13)];
if(a1==a2){
a2=cardSuit[(int)(Math.random()*4)]+cardNum[(int)(Math.random()*13)];
}
a3=cardSuit[(int)(Math.random()*4)]+cardNum[(int)(Math.random()*13)];
if(a1==a3||a2==a3){
a3=cardColor[(int)(Math.random()*4)]+cardNum[(int)(Math.random()*13)];
}
System.out.println( "随意抽出三张牌为: "+a1+ "; "+a2+ "; "+a3+ "; ");
}
}
[解决办法]
import java.math.*;
public class Card{
public static String[] cardSuit=new String[4];
public static String[] cardNum=new String[13];
static{
cardSuit[0]= "方块 ";
cardSuit[1]= "梅花 ";
cardSuit[2]= "红桃 ";
cardSuit[3]= "黑桃 ";
cardNum[0]= "A ";
for(int i=1;i <10;i++){
cardNum[i]=String.valueOf(i+1);
cardNum[10]= "J ";
cardNum[11]= "Q ";
cardNum[12]= "K ";
}
}
public static void main(String args[]){
String a1,a2,a3;
a1=cardSuit[(int)(Math.random()*4)]+cardNum[(int)(Math.random()*13)];
a2=cardSuit[(int)(Math.random()*4)]+cardNum[(int)(Math.random()*13)];
if(a1==a2){
a2=cardSuit[(int)(Math.random()*4)]+cardNum[(int)(Math.random()*13)];
}
a3=cardSuit[(int)(Math.random()*4)]+cardNum[(int)(Math.random()*13)];
if(a1==a3||a2==a3){
a3=cardSuit[(int)(Math.random()*4)]+cardNum[(int)(Math.random()*13)];
}
System.out.println( "随意抽出三张牌为: "+a1+ "; "+a2+ "; "+a3+ "; ");
}
}
///说明:由于你的Main方法是static 的,而static方法只能引用类的static成员和方法,建议好好看看static相关的内容