我怎样才能预先确定阵列?

时间:2014-03-05 17:18:03

标签: java arrays jpanel

我不知道这是否可行,但我正在尝试使用下拉列表制作JPanel。其中一个参数采用了一系列选项,因此我必须使用数组,而不是列表。

我现在正在这样做:

        //if they have enough gold to buy 5
        if(totalGold >= 25){
            Object[] possibilities = {1, 2, 3, 4, 5};
        }
        //if they have between 20 and 24 gold
        else if(totalGold >= 20 && totalGold < 25){
            Object[] possibilities = {1, 2, 3, 4};
        }
        //if Player has between 15 and 19 gold
        else if(totalGold >=15 && totalGold < 19){
            Object[] possibilities = {1, 2, 3};
        }
        //if Player has between 10 and 14 gold
        else if(totalGold >= 10 && totalGold <14){
            Object[] possibilities = {1, 2};
        }
        //if Player has between 5 and 9 gold
        else if(totalGold >= 5 && totalGold < 9){
            Object[] possibilities = {1};
        } 

不幸的是,可能性变量超出范围,我无法使用此变量。我希望能说出类似的话:

Object[] possibilities;

然后定义稍后的数组,但我似乎无法做到这一点。

有没有办法能够根据totalGold的值更改数组中包含的可能性?

谢谢!

3 个答案:

答案 0 :(得分:3)

注意totalGold和数组大小之间的模式。大小与totalGold % 5的{​​{1}}相同,而对于任何更大的值,大小只是totalGold <= 24

你可以利用它:

5

这是有效的,因为数组中的值是从int arraySize = Math.min(totalGold % 5, 5); // Why Object[]? int[] possibilities = new int[arraySize]; for (int i = 0; i < arraySize; i++) { possibilities[i] = i + 1; } 开始的。

答案 1 :(得分:1)

事先声明你的“可能性”,然后在if语句中指定正确的“可能性”。

       Object[] possibilities1 = {1, 2, 3, 4, 5};
       Object[] possibilities2 = {1, 2, 3, 4};
       Object[] possibilities3 = {1, 2, 3 };
       Object[] possibilities4 = {1, 2};
       Object[] possibilities5 = {1};

       Object[] possibilities;

       //if they have enough gold to buy 5
        if(totalGold >= 25){
            possibilities = possibilities1;
        }
        //if they have between 20 and 24 gold
        else if(totalGold >= 20 && totalGold < 25){
            possibilities = possibilities2;
        }
        //if Player has between 15 and 19 gold
        else if(totalGold >=15 && totalGold < 19){
           possibilities = possibilities3;
        }
        //if Player has between 10 and 14 gold
        else if(totalGold >= 10 && totalGold <14){
            possibilities = possibilities4;
        }
        //if Player has between 5 and 9 gold
        else if(totalGold >= 5 && totalGold < 9){
            possibilities = possibilities5;
        } 

但是,如果您可以动态生成数组及其内容Rohit Jain suggests in his answer,那么您应该采用这种方法。

答案 2 :(得分:0)

Rohit Jain的答案是要走的路。但我会解决你的实际问题,以解释发生了什么。

我猜你试过这样的事情:

Object[] possibilities;

//if they have enough gold to buy 5
if(totalGold >= 25){
    possibilities = new Object[]{1, 2, 3, 4, 5}; // needs to have 'new Object[]'
}
//if they have between 20 and 24 gold
else if(totalGold >= 20 && totalGold < 25){
    possibilities = new Object[]{1, 2, 3, 4};
}
//if Player has between 15 and 19 gold
else if(totalGold >=15 && totalGold < 19){
    possibilities = new Object[]{1, 2, 3};
}
//if Player has between 10 and 14 gold
else if(totalGold >= 10 && totalGold <14){
    possibilities = new Object[]{1, 2};
}
//if Player has between 5 and 9 gold
else if(totalGold >= 5 && totalGold < 9){
    possibilities = new Object[]{1};
}

doSomethingWithPossibilities(possibilities);

您可能遇到错误,possibilities可能尚未初始化,这是真的。如果possibilitiestotalGold,那么3是什么?

解决方案是添加else以确保无论if/else if/else如何评估,possibilities总是以值结束(或者方法以异常退出)。

Object[] possibilities;

//if they have enough gold to buy 5
if(totalGold >= 25){
    possibilities = new Object[]{1, 2, 3, 4, 5};
}

[snip]

//if Player has between 5 and 9 gold
else if(totalGold >= 5 && totalGold < 9){
    possibilities = new Object[]{1};
}
else {
    possibilities = new Object[]; //or null, or throw exception, or...
}

doSomethingWithPossibilities(possibilities);