Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我怎样才能预先声明一个数组?_Java_Arrays_Jpanel - Fatal编程技术网

Java 我怎样才能预先声明一个数组?

Java 我怎样才能预先声明一个数组?,java,arrays,jpanel,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 a

我不知道这是否可行,但我正在尝试制作一个带有下拉列表的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};
        } 
然后稍后定义数组是什么,但我似乎无法做到这一点

是否有一种方法可以根据totalGold的值更改可能性数组包含的内容


谢谢

注意
totalGold
和数组大小之间的模式。对于
totalGold,大小与
totalGold%5
相同。请注意
totalGold
与数组大小之间的模式。大小与
totalGold%5
相同,用于
totalGold事先声明“可能性”,然后在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;
        } 
Object[]可能性1={1,2,3,4,5};
对象[]可能性2={1,2,3,4};
对象[]可能性3={1,2,3};
对象[]可能性4={1,2};
对象[]可能性5={1};
目标[]可能性;
//如果他们有足够的黄金购买5
如果(总黄金>=25){
可能性=可能性1;
}
//如果他们有20到24黄金
否则如果(totalGold>=20&&totalGold<25){
可能性=可能性2;
}
//如果玩家拥有15到19枚金牌
否则如果(totalGold>=15&&totalGold<19){
可能性=可能性3;
}
//如果玩家拥有10到14枚金牌
否则如果(totalGold>=10&&totalGold=5&&totalGold<9){
可能性=可能性5;
} 

但是,如果您可以动态地生成数组及其内容,您可能应该采用这种方法。

事先声明您的“可能性”,然后在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;
        } 
Object[]可能性1={1,2,3,4,5};
对象[]可能性2={1,2,3,4};
对象[]可能性3={1,2,3};
对象[]可能性4={1,2};
对象[]可能性5={1};
目标[]可能性;
//如果他们有足够的黄金购买5
if(总计黄金>=25){
可能性=可能性1;
}
//如果他们有20到24黄金
否则如果(totalGold>=20&&totalGold<25){
可能性=可能性2;
}
//如果玩家拥有15到19枚金牌
否则如果(totalGold>=15&&totalGold<19){
可能性=可能性3;
}
//如果玩家拥有10到14枚金牌
否则如果(totalGold>=10&&totalGold=5&&totalGold<9){
可能性=可能性5;
} 

但是,如果您可以动态地生成数组及其内容,您可能应该采用这种方法。

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

罗希特·贾因的答案是正确的。但我会回答你的实际问题来解释发生了什么

我猜你试过这样的方法:

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

是什么让人觉得你不能那样做?可以在
if
块之外声明
可能性
,只要保证为其赋值(即,添加一个
else
,该值将在所有其他条件都失败时运行)。@FiveNine您不能在赋值操作中使用“数组常量”。仅当声明数组var.@ColinD Right时,它必须是
posabilities=new Object[]{1,2..}
。我的观点是,您当然可以在
if
块之外声明
可能性
,并稍后为其赋值。是什么让您看起来不能这样做?可以在
if
块之外声明
可能性
,只要保证为其赋值(即,添加一个
else
,该值将在所有其他条件都失败时运行)。@FiveNine您不能在赋值操作中使用“数组常量”。仅当声明数组var.@ColinD Right时,它必须是
posabilities=new Object[]{1,2..}
。我的观点是,您当然可以在
if
块之外声明
可能性
,然后为其赋值。谢谢!我甚至没有想过要做这样的事。我之所以使用object,是因为Oracle的例子就是这么用的。谢谢!我甚至没有想过要做这样的事。我之所以使用object,是因为Oracle的示例就是这么用的。谢谢!我已经很久没有使用数组而不是列表了,以至于我已经忘记了使用OneHanks可以做的一切!我已经很久没有使用数组而不是列表了,我已经忘记了使用数组可以做的一切
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);