在Java中比较同一多维数组的两个不同字符串元素

问题描述 投票:0回答:1

我目前正在为即将到来的实践考试做练习题,但遇到了困难。我编写了一种方法来检查一组三张卡片是否有效或无效,其中我们被告知“如果两张卡片没有相同的形状、颜色或图案,即它们的属性都不匹配,则它们是不同的。两张卡片如果它们具有相同的形状、颜色和图案,即所有属性都匹配,则它们是相同的。如果一组三张牌由 (i) 三张不同的牌,或 (ii) 三张相同的牌组成,则该套三张牌是有效的。”目前,如果我运行我的程序,每组三张卡都会被评估为“有效”。我很确定问题不在于我读入和存储卡片及其属性的方式,因为我已经对此进行了调试,所以我假设问题在于我的 cardsAreValid 方法的逻辑,特别是如何我比较每张卡的不同字符串元素(属性)。然而,我可能是错的。请帮忙。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Check {

    public static boolean cardsAreValid(String [][] threeCards){

        boolean sameShape = threeCards[0][0].equals(threeCards[1][0]) && threeCards[1][0].equals(threeCards[2][0]);
        boolean sameColour = threeCards[0][1].equals(threeCards[1][1]) && threeCards[1][1].equals(threeCards[2][1]);
        boolean samePattern = threeCards[0][2].equals(threeCards[1][2]) && threeCards[1][2].equals(threeCards[2][2]);

        if (sameShape == true && sameColour==true && samePattern==true) // if all attributes match - same so valid
            return true;
        else if (sameShape==false && sameColour==false && samePattern==false) // if no attributes match - distinct so valid
            return true;
        else
            return false; // if only 1 or 2 cards differ/match its invalid
    }
        
    public static void main (String [] args) {
            
        File inFile = new File("cards.txt");
        String [][] cards = new String[3][3];
        List<String> allLines = new ArrayList<>();
        
        try (Scanner sc = new Scanner(inFile)) {

            while (sc.hasNextLine()) {

                allLines.add(sc.nextLine());
            }  
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        for (int i = 0; i < allLines.size(); i++) {

            String fullLine = allLines.get(i);
            System.out.println("Processing: " + fullLine);

            String [] singleCard = fullLine.split(" ");

            for (int j = 0; j < 3; j++) {
                int firstComma = singleCard[j].indexOf(",", 0);
                int secondComma = singleCard[j].indexOf(",", firstComma+1);

                String shape = singleCard[j].substring(0, firstComma);
                String colour = singleCard[j].substring(firstComma+1, secondComma);
                String pattern = singleCard[j].substring(secondComma+1);

                cards[j][0] = shape;
                cards[j][1] = colour;
                cards[j][2] = pattern;
            }

            if (cardsAreValid(cards) == true) {
                    System.out.println("Valid");
            }
            else
                System.out.println("Invalid");
        }
    }
}

以防万一,示例文本文件 cards.txt 如下所示:

方形、蓝色、圆点、红色、实心三角形、绿色、条纹
正方形、蓝色、圆点、红色、实心三角形、绿色、实心
方形,蓝色,现货 方形,蓝色,现货 方形,蓝色,现货 方形,蓝色,现货
方形、蓝色、现货三角形、绿色、条纹

java multidimensional-array string-comparison array-comparison
1个回答
0
投票

是的,你对我们的逻辑有问题。您当前正在检查您的卡片中是否有两张不同的卡片,因此如果

card1
card2
不同,但与
card3
相同,您将得到
valid
。因为所有三个变量 (
sameShape
,
sameColour
,
samePattern
) 都将为 false。

尝试修改你的逻辑,这样它就会检查没有一张牌是相等的。 (您可能想要创建一个帮助函数,该函数将获取三个字符串并检查它们是否不相等。

© www.soinside.com 2019 - 2024. All rights reserved.