java - How to use an Array with an If statement -
i new java. i'm not sure how use array in java. may not able use correct terms attempt show in code. basically, array.
int[] arraycount = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
i set if function (assuming arraycount[1] default.... if array @ first state of [1], , "one".equals(match) sets array arraycount[2] , there on. basically, if "one" = match, should set arraycount 2, if "two" = match , first if statement has been executed, play test sound. chain go way 100, test.
for (string match : matches) { if (arraycount[1]== 1 && "one".equals(match)) { testsound.start(); arraycount[2]=2; } else if (arraycount[2]==2 && "two".equals(match)) { testsound.start(); }
hopefully i'm understanding question correctly. want user enter words, "one", "two", "three", etc in order, , @ each step of successful entry, play test sound?
in case, consider following:
import java.util.queue; import java.util.linkedlist; queue<string> inputs = new linkedlist<string>(); inputs.push("one"); inputs.push("two"); inputs.push("three"); // etc // check user input (string match : matches) { if (match.equals(inputs.peek())) { inputs.pop(); // removes element matched testsound.start(); } }
note assumes want take same action @ each step. if can describe requirements 'correct response' behavior little more, can provide more precise answer.
we use queue above, exhibits first-in-first-out ordering. means matches must appear in order added (all push statements above). inside loop, when successful match occurs, next desired match checked. instance, queue containing("three", "two", "one") , matches
containing ("one", "two", "thirty"), loop perform follows:
- match "one" compared head of queue, "one"
- this matches, "pop" head, leaving ("three", "two") in queue
- the next match, "two" compared head of queue (now "two")
- this matches, again pop head, leaving ("three") in queue
- the next match, "thirty" compared head of queue (now "three")
- this not match, no further changes occur queue
if want have specific behavior each of matches (i.e., when "one" matches, else when "two" matches, etc) wire following (in addition above)
public interface matchaction { public void dothething(); } map<string, matchaction> actionmap = new hashmap<string,matchaction>(); // fill bad boy actionmap.put("one", new matchaction() { public void dothething() { /* stuff */ } }); // etc each action (you can reuse instances here if actions same) // then, modify check above be: (string match : matches) { if (match.equals(inputs.peek())) { string input = inputs.pop(); matchaction action = actionmap.get(input); if (action != null) action.dothething(); } }
Comments
Post a Comment