我正在尝试使用indexOf方法使用整数来检查字符串是否等于字符串数组的值。这就是我所拥有的:
public int[] COMPONENT_IDS = { 18, 22, 20, 10, 12, 14, 16 };
public String[] COMPONENT_STRINGS = { "pie", "chocolate bar", "donut", "baguette", "triangle sandwich",
"sandwich", "bread" };
public int[][] REWARDS = { {995, 5000000}, {12852, 625000}, {8851, 1250000} };
public void handleComponents(int c) {
for(int i : COMPONENT_IDS) {
if(i == c) {
if(answer.equals(COMPONENT_STRINGS[COMPONENT_IDS.indexOf(i)]))
sendReward();
else
sendPunishment();
}
}
}
错误:
src\com\rs\game\player\content\AntiAFK.java:30: error: cannot find symbol
if(answer.equals(COMPONENT_STRINGS[COMPONENT_IDS
.indexOf(i)]))
^
symbol: method indexOf(int)
location: variable COMPONENT_IDS of type int[]
答案 0 :(得分:3)
只需将循环更改为旧式
for(int index=0; index < COMPONENT_IDS.length ; index ++){
//your code here
.
if(COMPONENT_IDS[index] == c) {
answer.equals(COMPONENT_STRINGS[index]);
.
.
}
答案 1 :(得分:1)