简体   繁体   中英

Running a standard Java code on Android

I want to run my Java code on Android. But I'm not familiar with Activities. How can I call WordPuzzle on an Activity?

Android activity:

public class Puzzle extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

Java code:

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

but i dont think System.out.println() will work, at least not where the user could view it. Change layout.main to a TextView and put your output there maybe

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM