作为本周作业(计算机编程I)的一部分,我应该研究我们在整个课堂上都在从事的UNO游戏。游戏只能在两台计算机之间进行,因此除了偶尔的输入才能使游戏继续进行之外,没有键盘输入(有点闷吗?)。这周我们应该为游戏的每个部分进行一些分类(例如CARD类和HAND类)。我能够做到这一点,但是我的任务的第二部分是拥有一个驱动程序:的UNO卡,将卡分发给两个或更多玩家,并显示每个卡的内容玩家的手。”-我被卡住了。我尝试了多种印刷方法,这些方法以我极其有限的技能来了解,但我什么都没想出来。有什么主意吗?
这里是代码:
public class CARD
{
public String color;
public int value;
private Random random;
private String face;
public CARD(int v, String c)
{
value = v;
color = c;
}
public CARD()
{
random = new Random();
value = random.nextInt(28); // 108 cards in a deck and it Can be reduced to 27 which ignores colors
// Assigns value
if (value >= 14) // Some cards show up more often (numbers)
value -= 14;
// Assigns color
random = new Random();
switch(random.nextInt(4) ) //learned about switches here: https://www.youtube.com/watch?v=RVRPmeccFT0
{
case 0: color = "Red";
break;
case 1: color = "Green";
break;
case 2: color = "Blue";
break;
case 3: color = "Yellow";
break;
}
// If the card is wild
if (value >= 13)
color = "none";
}
}
根据您提供的代码,我建议使用for循环制作卡:
public class DriverProgram {
static final int players = 2, numberOfCards = 7;
public static void main (String[] args){
java.util.ArrayList<CARD> c = new java.util.ArrayList<CARD>();
for(int i=0; i<numberOfCards; i++){
c.add(new CARD()); // The previous example used a fixed size array
} // Arrays.fill() would make all the cards identical
System.out.println(c);
// Note: ArrayLists print themselves nicely with their toString method.
// If you'd like, you could do String.replace("[", "").replace... to remove
// certain characters.
}
}
How do I print my Java object without getting "SomeType@2f92e0f4"?