方法不打印/返回

时间:2015-12-03 02:54:28

标签: java methods println

我的某个方法出了问题。我一遍又一遍地检查它,我似乎找不到任何错误。我遇到的问题是该方法在调用它时不会在控制台中打印。我认为它可能与方法中的扫描仪有关。

该方法的代码是:

public String cpuGuess() throws FileNotFoundException{
        Scanner fileRead = new Scanner(myGuesses);
        int line = rand.nextInt(99-1)+1;
        for(int i = 0; i <= line; i++){
            Guess = fileRead.next();
        }
        fileRead.close();
        CPUGuess = Guess;
        if(Arrays.asList(super.used).contains(CPUGuess)){
            System.out.println(cpu.cpuGuess());
        }
        else{
            StringBuffer hiddenBuff = new StringBuffer(hidden);
            for (int t = 0; t < phrase.length(); )
                if (CPUGuess == Character.valueOf(phrase.charAt(t)).toString()){
                    count++;
                }
            if (phrase.contains(CPUGuess)){
                ArrayList<Integer> indicies = new ArrayList<Integer>();
                for (int index = phrase.indexOf(CPUGuess.toString()); index >= 0; index = phrase.indexOf(CPUGuess.toString(), index + 1)){
                    indicies.add(index);
                }
                int[] index = new int[indicies.size()];
                for(int i = 0; i < indicies.size(); i++) {
                    index[i] = indicies.get(i);
                }
                for (int n = 0; n < index.length; n++){
                    hiddenBuff.setCharAt(index[n], CPUGuess.charAt(0));
                }
                hidden = hiddenBuff.toString();
                status = "Correct";
            }
            else if (!phrase.contains(CPUGuess)){
                System.out.println("Sorry, no " + Guess + "'s");
                System.out.println("Human, you're up!");
            }
            super.used.add(CPUGuess.charAt(0));
        }
        System.out.println(count + " " + CPUGuess + "s");
        count = 0;
        return hidden;
    }

我在main方法下调用main类中的方法:

public static void main(String[] args) throws FileNotFoundException {
    System.out.println("WHEEL... OF... FORTUNE!!!!");
    System.out.println(board.phraseGenerate());
    System.out.println(cpu.cpuGuess());
}

请注意,cpu对象已经构建,board.phraseGenerate会同时创建隐藏和短语。

感谢您的帮助,请不要居高临下,我相对较新,并且我看到很多帖子得到令人沮丧的反馈。

2 个答案:

答案 0 :(得分:1)

您必须在此for循环中增加t

for (int t = 0; t < phrase.length(); )
                if (CPUGuess == Character.valueOf(phrase.charAt(t)).toString()){
                    count++;
                }

另外,你错过了一个开放的花括号......它发生在我们最好的人身上。最后,增量和大括号:

for (int t = 0; t < phrase.length(); t++ ) { //Need that curly brace!

                   if (CPUGuess == Character.valueOf(phrase.charAt(t)).toString()){
                        count++;

     } 
}

                //t++ Probably important...!

答案 1 :(得分:0)

查看你的代码,你的for循环后似乎缺少一个开口花括号,如下所示:

 for (int t = 0; t < phrase.length(); ) 

应该是

 for (int t = 0; t < phrase.length(); ) { } 

以上原因导致跳过print语句。不要忘记你正在跳过我在评论中提到的循环的第三部分(增量)。不确定这是你的代码应该如何工作,或者你必须在那里添加一个t ++,如下所示:

//not sure if you forgot t++ or that's how it is supposed to work
for (int t = 0; t < phrase.length(); t++) { }