在Android上运行标准Java代码
我想在Android上运行我的Java代码。但我对活动不熟悉。如何在活动中调用WordPuzzle Android活动:在Android上运行标准Java代码,java,android,Java,Android,我想在Android上运行我的Java代码。但我对活动不熟悉。如何在活动中调用WordPuzzle Android活动: public class Puzzle extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } Java代
public class Puzzle extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
Java代码:
public class WordPuzzle {.....
public Set<String> findWords(char[][] puzzle, Set<String> words) {.....
private int findMinimumWordLength(Set<String> words) {.....
private Set<String> findPossibleWords(char[][] puzzle, int minimumWordLength) {.....
public void main(String args[]) {
WordPuzzle program = new WordPuzzle();
char[][] puzzle = {
{'F','Y','Y','H','N','R','D'},
{'R','L','J','C','I','N','U'},
{'A','A','W','A','A','H','R'},
{'N','T','K','L','P','N','E'},
{'C','I','L','F','S','A','P'},
{'E','O','G','O','T','P','N'},
{'H','P','O','L','A','N','D'}
};
Set<String> words = new HashSet<String>();
words.add("FRANCE");
words.add("POLAND");
words.add("JAPAN");
words.add("HOLLAND");
Set<String> wordsFound = program.findWords(puzzle, words);
for(String word : wordsFound) {
System.out.println(word);
}
}
}
}
公共类单词拼图{。。。。。
公共集合findWords(char[][]拼图,集合词){。。。。。
private int findMinimumWordLength(设置字){。。。。。
私有集findPossibleWords(char[][]字谜,int minimumWordLength){。。。。。
公共void main(字符串参数[]){
WordPuzzle程序=新的WordPuzzle();
字符[][]拼图={
{'F','Y','Y','H','N','R','D'},
{'R','L','J','C','I','N','U'},
{'A','A','W','A','A','H','R'},
{'N','T','K','L','P','N','E'},
{'C','I','L','F','S','A','P'},
{'E','O','G','O','T','P','N'},
{'H','P','O','L','A','N','D'}
};
Set words=新HashSet();
词语。添加(“法国”);
文字。添加(“波兰”);
字。添加(“日本”);
字。加上(“荷兰”);
Set wordsFound=program.findWords(拼图,单词);
for(字符串字:wordsFound){
System.out.println(word);
}
}
}
}
但是我认为System.out.println()
不起作用,至少在用户可以查看的地方不起作用。
将layout.main更改为
TextView
,并将输出放在那里,也许您希望它做什么?您可以只做WordPuzzle wp=new WordPuzzle();wp.findWords(..);需要将其设置为“public static void main(String args[])”否则你必须创建一个实例,然后再创建另一个实例,这是没有意义的。我已经重新格式化了你的答案以修复代码块。希望你不要介意。哈哈,在我单击“编辑”的同时,我感到困惑…我发誓它刚刚坏了…谢谢你,不,我来了
public class Puzzle extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WordPuzzle wp = new WordPuzzle(); // you need to make main() a static function to avoid this
String args[] = {""};
wp.main(args);
}