【问题标题】:Arraylist look for specific Element [duplicate]Arraylist 查找特定元素 [重复]
【发布时间】:2016-10-05 17:41:52
【问题描述】:

我正在尝试玩二十一点游戏,但如果 11 太高,我不知道如何将“ACE”的值更改为 1。我现在正在尝试在 Arraylist 中搜索 A,以更改其中一个或多个的值。但是如何才能看到我的 Arraylist 中有多少 A?

      while (getPointsPC() < 17 || getPointsPC() < getPointsPL()){

            int acees = 0;

            random = bj.getRandom();
            CardsPC.add(bj.getCard(random));
            setPointsPC(random);

            lblPointsPC.setText("Points Dealer: " + Integer.toString(getPointsPC()));

            String text = CardsPC.get(0);
            for(int i = 1; i < CardsPC.size(); i++){
                text = text + ", " + CardsPC.get(i);
            }

            if (CardsPC.contains("A") && getPointsPC() > 21 && acees < CardsPC.**HOW_MUCH_A's???**){
                setPointsPC(13);
                acees++;
            }

            lblCardsPC.setText(text);

        }

【问题讨论】:

  • 我不知道“ASS”有号码。您指的是“ACE”吗?

标签: java arraylist


【解决方案1】:

你的CardsPC是你提到的ArrayList吗?只需设置计数器变量:

String text = "";
int counter = 0;
for(int i = 0; i < CardsPC.size(); i++){
     text = text + (i > 0 ? ", " : "") + CardsPC.get(i);
     if (CardsPC.get(i).contains("A")) { // or use equals, base on your input.
          counter++;
          // do something with found "A" element if you want.
     }
}

【讨论】:

    猜你喜欢
    • 2021-03-19
    • 2019-09-30
    • 1970-01-01
    • 2020-06-24
    • 1970-01-01
    • 2021-04-04
    • 2014-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多