Java 如何使用带有 If 语句的数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16555161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use an Array with an If statement
提问by Nitan Shalom
I am new to Java. I'm not really sure how to effectively use an array in Java. I may not be able to use the correct terms so I will attempt to show you in code. Basically, this is my array.
我是 Java 新手。我不太确定如何在 Java 中有效地使用数组。我可能无法使用正确的术语,因此我将尝试在代码中向您展示。基本上,这是我的数组。
int[] arrayCount = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
I would like to set up my if function so that (assuming that arrayCount[1] is the default.... If that array is at that first state of [1], and "one".equals(match) then it sets the array to arrayCount[2] and then from there on. Basically, if "one" = match, it should set arrayCount to 2, if "two" = match AND the first if statement has already been executed, it will play the test sound. Ultimately this chain would go all the way up to 100, but this is just to test.
我想设置我的 if 函数,以便(假设 arrayCount[1] 是默认值......如果该数组处于 [1] 的第一个状态,并且“one”.equals(match) 然后它设置数组到arrayCount[2],然后从那里开始。基本上,如果“one”=匹配,它应该将arrayCount设置为2,如果“两个”=匹配并且第一个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();
}
采纳答案by Ron Dahlgren
Hopefully I'm understanding the question correctly. You want to user to enter the words, "one", "two", "three", etc in order, and at each step of a successful entry, play a test sound?
希望我能正确理解这个问题。您想让用户依次输入“一”、“二”、“三”等单词,并在成功输入的每一步播放测试声音?
In that case, consider the 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
// 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();
}
}
Note that this assumes you would want to take the same action at each step. If you can describe your requirements for 'correct response' behavior a little more, I can provide a more precise answer.
请注意,这假设您希望在每一步都采取相同的操作。如果您可以多描述一下您对“正确响应”行为的要求,我可以提供更准确的答案。
We use a Queue above, as it exhibits First-In-First-Outordering. This means that the matches must appear in the order they are added (all the push statements above). Inside the loop, when a successful match occurs, the next desired match will be checked. For instance, with a Queue containing("three", "two", "one") and matches
containing ("one", "two", "thirty"), the loop will perform as follows:
我们使用上面的队列,因为它展示了先进先出的顺序。这意味着匹配项必须按照它们添加的顺序出现(上面的所有 push 语句)。在循环内部,当成功匹配时,将检查下一个所需的匹配。例如,对于包含("three", "two", "one") 和matches
包含("one", "two", "thirty")的队列,循环将执行如下:
- match "one" will be compared to the head of the queue, "one"
- This matches, so we "pop" the head, leaving ("three", "two") in the queue
- The next match, "two" will be compared to the head of the queue (now "two")
- This matches, so we again pop the head, leaving ("three") in the queue
- The next match, "thirty" will be compared with the head of the queue (now "three")
- This does not match, so no further changes occur with the queue
- 匹配“一”将与队列的头部进行比较,“一”
- 这匹配,所以我们“弹出”头部,在队列中留下(“三”,“二”)
- 下一场比赛,“二”将与队列的头部进行比较(现在是“二”)
- 这匹配,所以我们再次弹出头部,在队列中留下(“三”)
- 下一场比赛,“三十”将与队列的头部进行比较(现在是“三”)
- 这不匹配,因此队列不会发生进一步的变化
If you want to have specific behavior for each of the matches (i.e., do something when "one" matches, then something else when "two" matches, etc) you could wire up something like the following (in addition to the above)
如果您想为每个匹配项设置特定的行为(即,在“一个”匹配时执行某些操作,然后在“两个”匹配时执行其他操作等),您可以连接以下内容(除了上述内容)
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();
}
}
回答by Kitesurfer
Basicly what you looking for is an HasMap like this:
基本上你要找的是这样的 HasMap:
Map<String,Integer> map = new HashMap<String,Integer>();
map.put("one",1);
map.put("two",2);
hth
第
回答by BLuFeNiX
"If that array is at that first state of [1] ... it sets the array to arrayCount[2]" doesn't make sense. Arrays do not have "states".
“如果该数组处于 [1] 的第一个状态......它将数组设置为 arrayCount[2]”是没有意义的。数组没有“状态”。
An array is an easy way to make a lot of variable or objects of the same type. Instead of declaring 5 int
variables like this:
数组是创建大量相同类型的变量或对象的简单方法。而不是int
像这样声明 5 个变量:
int num1 = 234;
int num2 = 635;
int num3 = 3568;
int num4 = 23;
int num5 = 745;
You can do this:
你可以这样做:
int[] nums = {234, 635, 3568, 23, 745};
And then you can reference them like this:
然后你可以像这样引用它们:
System.out.println(nums[2]);
This would print the number 3568
(since the array is 0-based, nums[0]
would be 234
).
这将打印数字3568
(因为数组是基于 0 的,所以nums[0]
将是234
)。
Please explain what you want to accomplish (try not to use code in your description), and I will give you some code that does what you want.
请解释你想完成什么(尽量不要在你的描述中使用代码),我会给你一些代码来完成你想要的。
回答by Larry McKenzie
From what I can understand you may want to create an arraylist like this:
据我所知,您可能想要创建一个这样的数组列表:
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...