java - 检查 java 字符串中的字符数是否正确

标签 java arrays string split

我正在尝试计算 java 字符串中出现的字符数。

例如:

给定扑克牌 6s/3d/2H/13c/Ad

/字符出现了多少次? = 4

用户可以输入不同数量的牌并改变牌变量的数量,因此硬编码方法来检查出现情况是行不通的。

分隔符可以是以下任意一种: -/space(一只手只允许使用一种分隔符类型)。 因此,我需要能够检查任一分隔符是否出现 4 次,否则给出的格式不正确。

这里有一些java代码可以让我更好地了解我正在尝试做的事情:

    String hand = "6s/1c/2H/13c/Ad";
    System.out.println("Original hand: " + hand);

    // split the hand string into individual cards
    String[] cards = hand.split(hand);

    // Checking for separators
    // Need to check for the correct number of separators
    if(hand.contains("/")){
        cards = hand.split("/");
    } else if (hand.contains("-")){
        cards = hand.split("-");
    } else if (hand.contains(" ")){
        cards = hand.split(" ");
    } else {
        System.out.println("Incorrect format!");

    }

任何帮助都会很棒!

这也是一个学校项目/作业。

编辑1--------------------------------------------------------------------

好的,这是根据您的建议我的代码

    String hand = "6s 1c/2H-13c Ad";
    System.out.println("Original hand: " + hand);

    // split the hand string into individual cards
    String[] cards = hand.split("[(//\\-\\s)]");

    if (cards.length != 5) {
        System.out.println("Incorrect format!");    
    } else {

        for (String card : cards) {
            System.out.println(card);
        }
    }

上面给定的手牌格式不正确,因为用户只能对给定的手牌使用一种类型的分隔符。例如:

  • 6s/1c/2H/13c/Ad - 正确
  • 6s-1c-2H-13c-Ad - 正确
  • 6s 1c 2H 13c 广告 - 正确

如何确保用户只使用一种类型的分隔符?

为迄今为止的答案干杯!

编辑2 ------------------------------------------------------

所以使用嵌套的 if 语句我的代码现在看起来像这样:

    String hand = "6s/1c/2H/13c/Ad";
    System.out.println("Original hand: " + hand);

    // split the hand string into individual cards

    if(hand.contains("/")){
        String[] cards = hand.split("/");
        if(cards.length != 5){
            System.out.println("Incorrect format! 1");
        } else {
            for (String card : cards) {
                System.out.println(card);
            }
        }

    } else if(hand.contains("-")){
        String[] cards = hand.split("-");
        if(cards.length != 5){
            System.out.println("Incorrect format! 2");
        } else {
            for (String card : cards) {
                System.out.println(card);
            }
        }

    } else if(hand.contains(" ")){
        String[] cards = hand.split(" ");
        if(cards.length != 5){
            System.out.println("Incorrect format! 3");
        } else {
            for (String card : cards) {
                System.out.println(card);
            }
        }
    } else {
        System.out.println("Incorrect format! 4");
    }

这种方式可以按预期工作,但很丑!

任何建议都会非常高兴。

最佳答案

在不泄露答案的情况下,您希望将分隔符指定为 split这样返回的字符串数组看起来像:

cards[0] = "6s"
cards[1] = "1c"
cards[2] = "2H"
.
.
.

在您特定的手牌字符串中,您可以使用每张牌方便的分隔符来实现此目的...

关于java - 检查 java 字符串中的字符数是否正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11806376/

相关文章:

java - 尽管设置了 maxWidth 属性,AnchorPane 仍在继续增长

java - 从方法创建并返回数组

arrays - Ruby - 基于另一个数组过滤哈希数组

python - 使用正则表达式排除字符串搜索中的字符?

arrays - 如何在我的数组中找到与正则表达式匹配的连续字符串?

java - Java中的图像差异

java - 无法通过 Java Maleorang 的 MailChimp API 3.0 包装器获取数据 - 404 错误

java - 如何在数据库中查询结果集中的一个字段? (JAVA ODBC)

c++ - 深拷贝int数组c++

java - 在java中复制字符串中的前N个单词