3

我是 Java 新手。我不太确定如何在 Java 中有效地使用数组。我可能无法使用正确的术语,因此我将尝试用代码向您展示。基本上,这是我的数组。

int[] arrayCount = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};

我想设置我的 if 函数,以便(假设 arrayCount[1] 是默认值....如果该数组处于 [1] 的第一个状态,并且“one”.equals(match) 然后它设置数组到 arrayCount[2] 然后从那里开始。基本上,如果 "one" = match,它应该将 arrayCount 设置为 2,如果 "two" = match 并且第一个 if 语句已经执行,它将播放测试声音。最终这条链会一直上升到 100,但这只是为了测试。

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

                }
4

4 回答 4

3

希望我能正确理解这个问题。您想让用户按顺序输入“一”、“二”、“三”等单词,并在成功输入的每一步播放测试声音?

在这种情况下,请考虑以下事项:

import java.util.Queue;
import java.util.LinkedList;

Queue<String> inputs = new LinkedList<String>();
inputs.push("one");
inputs.push("two");
inputs.push("three");
// etc
// Then to check the user input
for (String match : matches) {
  if (match.equals(inputs.peek())) {
    inputs.pop(); // Removes the element you just matched
    testSound.start();
  }
}

请注意,这假设您希望在每个步骤中采取相同的操作。如果您可以更多地描述您对“正确响应”行为的要求,我可以提供更准确的答案。

我们使用上面的队列,因为它展示了先进先出的顺序。这意味着匹配项必须按照它们添加的顺序出现(上面的所有推送语句)。在循环内部,当匹配成功时,将检查下一个所需匹配。例如,对于包含(“三”、“二”、“一”)和matches包含(“一”、“二”、“三十”)的队列,循环将执行如下:

  1. match "one" 将与队列的头部进行比较,"one"
  2. 这匹配,所以我们“弹出”头部,将(“三”,“二”)留在队列中
  3. 下一场比赛,“二”将与队列的头部(现在是“二”)进行比较
  4. 这匹配,所以我们再次弹出头部,将(“三”)留在队列中
  5. 下一场比赛,“三十”将与队列的头部(现在是“三”)进行比较
  6. 这不匹配,因此队列不会发生进一步的变化

如果您想对每个匹配项都有特定的行为(即,当“一个”匹配时做某事,然后在“两个”匹配时做其他事情等),您可以连接如下内容(除了上述内容)

public interface MatchAction {
  public void doTheThing();
}

Map<String, MatchAction> actionMap = new HashMap<String,MatchAction>();
// Fill this bad boy up
actionMap.put("one", new MatchAction() { public void doTheThing() { /* do stuff */ } });
// Etc for each action (you can reuse instances here if some actions are the same)
// Then, we modify the check above to be:
for (String match : matches) {
  if (match.equals(inputs.peek())) {
    String input = inputs.pop();
    MatchAction action = actionMap.get(input);
    if (action != null) action.doTheThing();
  }
}
于 2013-05-15T01:25:44.820 回答
1

基本上你要找的是这样的 HasMap:

Map<String,Integer> map = new HashMap<String,Integer>();
map.put("one",1);
map.put("two",2);

hth

于 2013-05-15T01:06:38.317 回答
0

“如果该数组处于 [1] 的第一个状态......它将数组设置为 arrayCount [2]”没有意义。数组没有“状态”。

数组是一种创建许多相同类型的变量或对象的简单方法。而不是像这样声明 5 个int变量:

int num1 = 234;
int num2 = 635;
int num3 = 3568;
int num4 = 23;
int num5 = 745;

你可以这样做:

int[] nums = {234, 635, 3568, 23, 745};

然后你可以像这样引用它们:

System.out.println(nums[2]);

这将打印数字3568(因为数组是从 0 开始的,nums[0]所以是234)。

请解释你想要完成的事情(尽量不要在你的描述中使用代码),我会给你一些你想要的代码。

于 2013-05-15T01:18:19.563 回答
0

据我所知,您可能想要创建一个这样的数组列表:

String[] array = ["zero", "one", "two"];
ArrayList<String> mArrayList = new ArrayList<String>(Arrays.asList(array));
int index = mArrayList.indexOf(user_input);
//use the index to play the appropriate sound...
于 2013-05-15T01:48:29.130 回答