Fun Game


//Takes Input and checks last character to the first character in the  dictionary
//if matches print it


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package Practise;

//Takes Input and checks last character to frst character in the  dictionary
//if matches pritnts it
import java.util.HashMap;
import java.util.Map;

public class Game {
 public static void main(String[] args) {
  Game game = new Game();
  String[] dictionary = { "asx", "xavier", "nihal", "logir", "tigek", "rabbit", "raddit", "kinp", "pia", "bscuit",
    "nikhil" };
  game.gameMethod("cidn", dictionary);
 }

 Map<String, Integer> map = new HashMap<String, Integer>();

 public void gameMethod(String str, String[] dictionary) {
  if (dictionary == null) {
   System.out.println("empty dictionary");
  }
  char ch = str.toUpperCase().charAt(str.length() - 1);
  for (int j = 0; j <= dictionary.length - 1; j++) {
   String inp = dictionary[j];
   if (ch == inp.toUpperCase().charAt(0)) {
    if (!map.isEmpty() && map.get(inp) != null && map.get(inp) == 1) {
     continue;
    }
    System.out.println(inp);
    map.put(inp, 1);
    gameMethod(inp, dictionary);

   }
  }
 }
}

Output:
 nihal
logir
rabbit
tigek
kinp
pia
asx
xavier
raddit
nikhil




No comments:

Post a Comment