无法访问代码的问题

时间:2014-04-09 16:44:57

标签: java

试图将山羊添加到arraylist。我在评论中写了我的错误。 Arraylist无法解析输入。  和无法访问的代码。

●如果该角色已被猜到,则应抛出IllegalArgumentException 并显示一条消息的信已经猜到

●如果字符不是来自AZ的字母(从信函中借用if代码) 构造函数第6步下的模型类),它应该抛出 抛出:IllegalArgumentException有消息称猜测应该是一个字母 从AZ

package edu.htc.java1.phrasegame;
import edu.htc.java1.phrasegame.model.*;

import java.util.ArrayList;

public class PhraseGameController { 

    private Phrase currentPhrase; 
    private ArrayList<Character> guessed; 




    public boolean doPlayerGuess(Character play) {
      Character goat = Character.toUpperCase(play);
        if(guessed.contains(play)){
            throw new IllegalStateException("the letter was already guessed");}
            else if {String.valueOf(play).matches("[A-Z]"); 
            throw new IllegalStateException("the guess should be a letter from A­Z");

        }
        guessed.add(goat);  // Unreachable Code
        return currentPhrase.guessLetter(goat);

    }






    public PhraseGameController(){
    currentPhrase = new Phrase("This is only a test");
    guessed = new ArrayList<Character>();       ///Arraylist could not be resolved to Type
    }



     public Phrase getCurrentPhrase() {  
        return currentPhrase;
    }
    public void setCurrentPhrase(Phrase currentPhrase) {
        this.currentPhrase = currentPhrase;
    }

    public void setGuessed(ArrayList<Character> guessed) {
        this.guessed = guessed;
    }
    public ArrayList<Character> getGuessed() { 
        return guessed;
    }
}

...

  

&GT;

 package edu.htc.java1.phrasegame.model;
> 
> import java.util.ArrayList;
> 
> public class Phrase {
>         // public String phrase;    ArrayList<Letter> letters = new ArrayList<Letter>();    private String phrase;
>     
> 
>     public Phrase(String phrase) {      phrase = phrase.toUpperCase(); ////
> Problem         for(char c : phrase.toCharArray()) {            letters.add(new
> Letter(c));         }       this.phrase = phrase;   }           public void
> setPhrase(String phrase) {      this.phrase = phrase;   }
> 
>     public String getPhrase() {         return phrase;  }
> 
>     public ArrayList<Letter> getLetters() {         return letters;     }
> 
>     public boolean guessLetter(char c) {
>             boolean foundLetter = false;
>             
>             // loop through your list of letters
>             for(Letter l : letters){
>                 // if list of letters contains same letter as the one you received then return true
>                 if(l.getLetter() == c) {
>                     l.unhide();
>                     foundLetter = true;
>                 }
>             }
> 
>             // we did not find the letter, so we return false
>             return foundLetter;         }   }

1 个答案:

答案 0 :(得分:2)

else {String.valueOf(play).matches("[A-Z]"); 

这不符合你的想法。

这就是说,如果没有猜到这个值,那么(总是)看看游戏是否匹配A-Z,忽略结果,然后(在你的下一行)总是抛出一个IllegalStateException

所以无论目前发生什么,都会抛出IllegalStateExcetpion,因此永远不会到达方法结尾的return语句。

您几乎肯定希望将上述代码包含在第二个if语句中,格式类似于:

else if(otherThingToCheck()) {
    //Code to execute if otherThingToCheck() is true
}