Java 基于串联字符串变量声明类;更改类变量的范围

Java 基于串联字符串变量声明类;更改类变量的范围,java,class,arraylist,Java,Class,Arraylist,我正在从事一个项目,其中一个Java程序能够正确猜测 命运之轮游戏中的单词,其中可以有任意数量的单词。 这些要猜测的单词来自一个由 名言一览表 我将每个单词表示为一个类,我持有要猜测的单词 在一大堆单词中。作为 所以我有点像 public class WheelOfFortune { private Set<String> wofTempWordSource; // this receives the world list private List<word> Sen

我正在从事一个项目,其中一个Java程序能够正确猜测 命运之轮游戏中的单词,其中可以有任意数量的单词。 这些要猜测的单词来自一个由 名言一览表

我将每个单词表示为一个类,我持有要猜测的单词 在一大堆单词中。作为

所以我有点像

public class WheelOfFortune
{


private Set<String> wofTempWordSource;
// this receives the world list
private List<word> SentenceElement = new ArrayList<word>();
// this array list contains and array list of the words in the sentence.


    public WheelOfFortune(Set<String> dictionary) 
    {
        // Implement me!
        wofTempWordSource = dictionary;
    } // end of WheelOfFortuneGuessSolver()
    
    
    
   public void newGame(int[] wordLengths) 
    { 
    
    int numSentenceElements = wordLengths.length;
        // this is the number of elements in the sentence.      
        
        String wrdName="";
        
        String wordName="";
        int wrdlngth=0;
        // This variable represents the word lens
        int wrdStrt=0;
        // This variable represents the index of the start of the word
        int wrdEnd=0;
        // This verbal represents the index of the end of the word
        int space =1;
        // this represents the one character to add for the space character 
        boolean wordGuessedCorrect = false;
        // This variable is a flag to indicate if the world has been 
        // successfully guessed of or not. 
        
        
        for (int wordnum = 1; wordnum <= numSentenceElements; wordnum++) 
        {
            wrdName = "w" + String.valueOf(wordnum);
            
            wordName= wrdName;
            
            wrdlngth = wordLengths[wordnum-1 ];
            
            
            
            word wordName =  new word(wrdName, wrdlngth, wrdStrt, wrdEnd, wordGuessedCorrect );
            SentenceElement.add(wordName  ) ;
            wordName.setTempWordSource(wofTempWordSource);

            
            
        }// close  for (int wordnum = 1; wordnum <= numSentenceElements; wordnum++) 
        
        
         
    
    }
    
    public char makeAGuess() 
    {
        // Implement me!

        return '\0';
    } // end of makeAGuess()
    
    
     public void Feedback()
    {
        // Implement me!
    }
    
    
    
    
    

}
因此,通过循环并创建word类的新实例作为

word W1 =  new word(wrdName, wrdlngth, wrdStrt, wrdEnd, wordGuessedCorrect );
SentenceElement.add(W1  ) ;
W1.setTempWordSource(wofTempWordSource);


word W2 =  new word(wrdName, wrdlngth, wrdStrt, wrdEnd, wordGuessedCorrect );
SentenceElement.add(W1  ) ;
W2.setTempWordSource(wofTempWordSource);

word W3  =  new word(wrdName, wrdlngth, wrdStrt, wrdEnd, wordGuessedCorrect );
SentenceElement.add(W1  ) ;
W3.setTempWordSource(wofTempWordSource);

etc.. 
  • 因为单词类W1、W2、W3已经在newGame()中声明,所以它们 只有该方法中的作用域,但我需要使它们全局可用 因此可以通过makeAGuess()、Feedback()访问它们。。。怎么做

  • #2您以前将wordName定义为字符串,现在将其重新定义为“word”。如果word是一个类,那么Java命名约定要求它以大写字母(word)开头。当然,但我希望能够动态声明“word”对象并使其全局可用…您可以使用ArrayList,但不能在Java中动态创建变量名。我不明白你想做什么。我正在尝试动态创建新的“Word”类实例,因为我不知道句子的长度,所以我不知道我必须声明并使其可用的实例的数量。。。。
    word W1 =  new word(wrdName, wrdlngth, wrdStrt, wrdEnd, wordGuessedCorrect );
    SentenceElement.add(W1  ) ;
    W1.setTempWordSource(wofTempWordSource);
    
    
    word W2 =  new word(wrdName, wrdlngth, wrdStrt, wrdEnd, wordGuessedCorrect );
    SentenceElement.add(W1  ) ;
    W2.setTempWordSource(wofTempWordSource);
    
    word W3  =  new word(wrdName, wrdlngth, wrdStrt, wrdEnd, wordGuessedCorrect );
    SentenceElement.add(W1  ) ;
    W3.setTempWordSource(wofTempWordSource);
    
    etc..