Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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_Integer_Dropdown - Fatal编程技术网

Java,整数下拉列表

Java,整数下拉列表,java,arrays,integer,dropdown,Java,Arrays,Integer,Dropdown,我试图有一个下拉列表,以要求用户的魅力修饰符 我目前正在使用JavaSwing提供的下拉列表。 我正在使用的下拉列表可以找到: 这是我的代码: import javax.swing.JOptionPane; public class Character_Charisma { private static String input = ""; public static int main() { int[] choices = {-4, -3, -2, -1,

我试图有一个下拉列表,以要求用户的魅力修饰符

我目前正在使用JavaSwing提供的下拉列表。 我正在使用的下拉列表可以找到:

这是我的代码:

import javax.swing.JOptionPane;

public class Character_Charisma
{
    private static String input = "";

    public static int main() {
        int[] choices = {-4, -3, -2, -1, 0, 1, 2, 3, 4, 5};
        int input = (int) JOptionPane.showInputDialog(null, "Choose now...",
            "What is your Charisma modifier?", JOptionPane.QUESTION_MESSAGE, null, 
                                                                    // Use
                                                                    // default
                                                                    // icon
            choices, // Array of choices
            choices[0]); // Initial choice
            return input;
    }
}
我正在努力用这段代码返回一个整数

我收到错误:“不兼容的类型:int[]无法转换为java.lang.Object[]”


我做错了什么?

改用Integer,因为它继承自Object类:

import javax.swing.JOptionPane;

public class Character_Charisma
{
    private static String input = "";

    public static int main() {
        Integer[] choices = {-4, -3, -2, -1, 0, 1, 2, 3, 4, 5};
        Integer input = (Integer) JOptionPane.showInputDialog(null, "Choose now...",
            "What is your Charisma modifier?", JOptionPane.QUESTION_MESSAGE, null, 
                                                                    // Use
                                                                    // default
                                                                    // icon
            choices, // Array of choices
            choices[0]); // Initial choice
            return input;
    }
}

你读过关于如何构造Swing GUI程序的教程吗?您缺少一些基础知识,您不能仅在非gui程序中实例化一个
JOptionPane
,需要一定数量的代码才能使gui和事件循环运行,而您忽略了这一点。我已经让Swing gui用于其他一些字符串输入。目前,它正在为我所需要的工作。我正在使用BlueJ来编写我的程序。非常感谢,我知道这很简单。我无法投票,因为我的声望不足15。不,你可以投票,可能是因为有人已经投了反对票!我怀疑他/她否决某项正确投票的理由。