0

尝试使用 Scanner 对象从 JOPtionPane 输入对话框中读取时遇到了困难(下面的文本)

int array[] = new int[6];
for (int i=0; i<6; i++)
    { 
        Scanner sc = new Scanner(System.in);
        JOptionPane.showInputDialog("Enter a number to fill the array: ");
        array[i]=sc.nextInt(System.in);
    }

更改如下代码后,困难得到解决:

String st;
int array[] = new int[6];
for (int i=0; i<6; i++)
    { 
        st= JOptionPane.showInputDialog("Enter a number to fill the array: ");
        array[i] = Integer.parseInt(st);
    }

我想知道为什么我不能使用 Scanner 类对象填充数组:/

4

1 回答 1

0

扫描仪可以设置为从多个不同的地方获取输入,但不幸的是不是来自 JOptionPane。如果您想使用扫描仪,您必须选择不同的输入方法。文件、现有字符串或控制台输入。

Java 6 API - 类扫描器

JOptionPane 仅从一个地方收集其输入,即它自己。

Java 6 API - 类 JOptionPane

于 2013-02-19T20:48:08.293 回答