否则!string [i] [j] .equals(name)将j次减去IF

时间:2014-03-08 22:28:18

标签: java multidimensional-array

行。完全披露,这是一项学校任务,我已经解决了一切,但这个未找到的陈述,我无法弄清楚我的生活。它要么打印999次并打印名称在正确的位置排名年份,并且我已经更改了斑点并将其移动了尝试了一段时间循环,添加更多。它必须是简单的东西,我无法接受它。

这个想法是搜索10个不同的文本文件,找到名称并打印它的排名(索引相当多)。我已经完成了所有工作,我明白该怎么做。我已经评论了if语句我遇到了麻烦。捕获后它向下移动。这是最后一段代码。我也明白为什么要这样做,因为在else中它正在经历循环并且它不等于999次,但是如果名称根本不匹配任何名称我只想打印它。

感谢您的帮助。

    public static void readNames() {
        for (int i = 0; i < row; i++) {
            String filename = "C:\\Users\\Rocketman\\Desktop\\JAVA\\babynameranking" + (2001 + i) + ".txt";
            try (Scanner input = new Scanner(new File(filename))) {
                for (int j = 0; j < column; j++) {
                    input.nextInt();//rank
                    boyNames[i][j] = input.next();//boy name
                    input.nextInt();//boy amount
                    girlNames[i][j] = input.next();//girl name
                    input.nextInt();//girl amount
                    if (((year == 2001 + i) && (i == 0 + i)) || (year == 2010 && i == 9)) {
                        if (gender.equalsIgnoreCase("m") || gender.equalsIgnoreCase("male")) {
                            if (boyNames[i][j].equalsIgnoreCase(name)) {
                                System.out.println(name + " rank is " + (j + 1) + ".");
                            }
                        } else if (girlNames[i][j].equalsIgnoreCase(name)) {
                            System.out.println(name + " rank is " + (j + 1) + ".");
                        }
                    }

                }
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            }
        }
       // if ((!boyNames.equals(name) || !girlNames.equals(name))) {
       //     System.out.println(name + " is not ranked in " + year);
       // }
    }
}

1 个答案:

答案 0 :(得分:1)

您需要在处理特定文件的块中使用boolean变量。将其初始化为false(此人未排名)。处理文件时,如果您发现他/她在该文件中排名,请将其设置为true。然后在最后检查该变量是真还是假(用该检查替换您的注释部分)。

通常,此检查!boyNames.equals(name)将不起作用,因为boyNames是一个2D数组,它永远不会等于Java中的字符串。

看看这是否可以解决您的问题。

    public static void readNames() {
        for (int i = 0; i < row; i++) {
            String filename = "C:\\Users\\Rocketman\\Desktop\\JAVA\\babynameranking" + (2001 + i) + ".txt";
            try (Scanner input = new Scanner(new File(filename))) {
                boolean ranked = false;
                for (int j = 0; j < column; j++) {
                    input.nextInt();//rank
                    boyNames[i][j] = input.next();//boy name
                    input.nextInt();//boy amount
                    girlNames[i][j] = input.next();//girl name
                    input.nextInt();//girl amount
                    if (((year == 2001 + i) && (i == 0 + i)) || (year == 2010 && i == 9)) {
                        if (gender.equalsIgnoreCase("m") || gender.equalsIgnoreCase("male")) {
                            if (boyNames[i][j].equalsIgnoreCase(name)) {
                                System.out.println(name + " rank is " + (j + 1) + ".");
                                ranked = true;
                            }
                        } else if (girlNames[i][j].equalsIgnoreCase(name)) {
                            System.out.println(name + " rank is " + (j + 1) + ".");
                            ranked = true;
                        }
                    }
                }
                if (!ranked){
                    System.out.println(name + " is not ranked in " + year);
                }
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            }
        }
    }
}