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:

  1. match "one" compared head of queue, "one"
  2. this matches, "pop" head, leaving ("three", "two") in queue
  3. the next match, "two" compared head of queue (now "two")
  4. this matches, again pop head, leaving ("three") in queue
  5. the next match, "thirty" compared head of queue (now "three")
  6. 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

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -