1

我找不到将其转换为 int 的方法。

这是代码

int array[] = new int [newsize];

    for (int a = 0; a < array.length; a++) {
        array[a] = JOptionPane.showInputDialog("Enter Value For Array["+a+"].");
    }

如何将其转换为 int ..(抱歉问题)

array[a] = JOptionPane.showInputDialog("Enter Value For Array["+a+"].");  

感谢您帮助首先将输入的字符串解析为 int

现在我有新问题。如何在一条 JOptionPnae 消息中显示 ZERO、POSITIVE、NEGATIVE 字符串以及相应的数组。

这是我的全新代码....

字符串显示=""; 整数 z = 0;

    String size = JOptionPane.showInputDialog("Enter Your Prefered Size Of Your Array");
    int newsize = Integer.parseInt(size);

    JOptionPane.showMessageDialog(null,"You Entered "+newsize+".");

    int array[] = new int [newsize];

    for (int a=0; a<array.length;a++)
    {
        array[a] = Integer.parseInt(JOptionPane.showInputDialog("Enter Value For Array["+a+"]."));

    }



    for (int a=0;a<array.length;a++)
    {
        display=display+array[a]+"\n";

        if (z == array[a])
        {
          String c=array[a]+"  ZERO";
          JOptionPane.showMessageDialog(null,"Arrays\n"+display+c);
        }
        else if (z < array[a])
        {
            String c =array[a]+"  POSITIVE";
            JOptionPane.showMessageDialog(null,"Arrays\n"+display+c);
        }
        else if (z != array[a])
        {
            String c =array[a]+"  NEGATIVE";
            JOptionPane.showMessageDialog(null,"Arrays\n"+display+c);
        }

        JOptionPane.showMessageDialog(null,"Arrays\n"+display+c);
     }

希望你能得到我的问题..

4

5 回答 5

0

尝试使用这个:

Integer.parseInt( JOptionPane.showInputDialog( "Enter Value For Array["+a+"]." ))
于 2013-11-13T14:36:56.220 回答
0

像下面这样

 for (int a = 0; a < array.length; a++) {
    String value = JOptionPane.showInputDialog("Enter Value For Array["+a+"].");
    if(value!=null){
          array[a] =  Integer.parseInt(value);
    }
 } 
于 2013-11-13T14:42:50.687 回答
0
array[a] = Integer.parseInt(JOptionPane.showInputDialog("Enter Value For Array["+a+"]."));

您还应该检查输入值是否为 int,否则将引发异常。

于 2013-11-13T14:51:05.853 回答
0

试试下面的代码:

String length = JOptionPane.showInputDialog("How many number do you want to put into a array?");

    //Declare a string array with the length entered.
    String[] array = new String[Integer.valueOf(length)];

    /*
     * Input numbers for array
     */
    for(int i =0 ; i<array.length; i++)
    {
         array[i] = JOptionPane.showInputDialog("Enter Value For Array["+i+"].");
    }

    /*
     *  Show Array information
     */

    JOptionPane.showMessageDialog(null, "Array elements are : "+ Arrays.toString(array));

在此处输入图像描述

于 2013-11-13T14:52:21.790 回答
0

您应该检查输入文本:

int[] array = new int[5];
for (int a = 0; a < array.length; ) {
    String inputText = JOptionPane.showInputDialog("Enter Value For Array[" + (a + 1) + "].");
    if (inputText == null) { // Cancel button pressed 
        break;
    }
    try {
        array[a] = Integer.valueOf(inputText);
    } catch (NumberFormatException ex) {
        JOptionPane.showMessageDialog(null, inputText + " is not a number. Try again!", "Error", JOptionPane.ERROR_MESSAGE);
        continue;
    }
    a++;
}
于 2013-11-13T14:55:52.997 回答