从不同的类中获取声明的变量

时间:2015-03-04 18:33:23

标签: java class variables public

好的,我的主类中有一个方法可以从文件中获取随机单词。我有一个表单,我希望能够从其他类中获取该随机Word。我对公共变量并不熟悉并且对它们缺乏了解。任何建议也是受欢迎的。谢谢!

public class Project {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) 
        throws IOException
{
    pickWord();
    frmPlaying.main(args);
    //To be honest I only know this shows the form and don't know why
}

public static void pickWord()
        throws IOException
{
    File words = new File("wordList.txt");
    String wordToArray = new String();
    String[] arrWord = new String[3863];   
    Scanner sc = new Scanner(words);
    Random rWord = new Random();
    int i = 0;


    do
    {
        sc.nextLine();
        wordToArray = sc.next();
        arrWord[i] = wordToArray;
        i++;    
    }while(sc.hasNext());

    Arrays.toString(arrWord);
    int idx = rWord.nextInt(arrWord.length);
    String randomWord = (arrWord[idx]);
    return randomWord;

}

}

^^^^获取随机单词

/* try
    {
        Project.pickWord();
    }
    catch(IOException ioe)
    {
        ioe.printStackTrace();
    }
    */

获取随机字的可疑代码

1 个答案:

答案 0 :(得分:0)

为您的方法提供返回类型。了解相关信息:http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html

http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

public static String pickWord()
    throws IOException{
....
String randomWord = (arrWord[idx]); /* Random Word I want this to be */
return randomWord;
}