到目前为止,我正在写一个21点游戏,它做了它应该做的事情,除了处理何时以及如何将ace计数为1或11。我已经尝试了两种方法。它们是:
public static int calcScore(ArrayList<Card> hand){
int sum = 0;
for (Card c : hand){
sum += c.getCardValue().getNumeric();
}
return sum;
}和
public static int calcScore(ArrayList<Card> hand){
Scanner aceHandler = new Scanner(System.in);
int sum = 0;
boolean countedAce = false;
for (Card c : hand){
if (!c.getCardValue().getFace().equals("A")){
sum += c.getCardValue().getNumeric();
}
else{
if (sum >= 11){
sum += 1;
}
else if (countedAce == false){
System.out.println("Do you want 1 or 10 for ace? \n Enter '1' or '11'");
String answer = aceHandler.nextLine();
if(answer.equals("1")){
sum+=1;
countedAce = true;
}
else{
sum+=11;
countedAce = true;
}
}
}
}
return sum;
} 上面的不能用Ace做任何事情,下面的在输入"1“或"11”后一直要求输入。我在main中运行终极游戏(String[] args)。我怎样才能让这两个方法中的任何一个去呢?
另外,我有时会遇到一个问题,我的dealIn(ArrayList hand)方法处理重复项。我试着设计它来避免它:
public static ArrayList<Card> dealIn(ArrayList<Card> hand){
Card a = new Card();
Card b = new Card();
hand.add( a);
if(!b.equals(a))
hand.add(b);
return hand;
}那个(!b.equals(a))子句应该避免它们,但有时并不是这样。我如何解决这个问题?
https://stackoverflow.com/questions/44608316
复制相似问题