2011-05-12 130 views
3

我想在Android上运行我的Java代码。但我不熟悉活动。如何在活动上调用WordPuzzle?在Android上运行标准Java代码

Android的活动:

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); 
      } 
     } 
    } 
} 
+0

你想要它做什么?你可以只是做WordPuzzle wp = new WordPuzzle(); wp.​​findWords(....); – 2011-05-12 20:57:05

+0

需要将其设置为“public static void main(String args [])”,否则您必须创建一个实例,然后创建另一个实例,并且没有意义。 – loosecannon 2011-05-12 21:03:47

回答

5
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); 
    } 

但我不认为System.out.println()会的工作,至少不要在那里用户可以查看它。 将layout.main更改为TextView,并将您的输出放在那里可能

+0

我已将您的答案重新格式化以修复代码块。我希望你不介意。 – 2011-05-12 21:02:05

+0

哈哈在同一时间我点击编辑,我很困惑....我发誓它只是坏了...谢谢你,虽然,我不懂我们 – loosecannon 2011-05-12 21:02:51