0

如何在一条 JOptionPane 消息中显示带有相应数组的零、正、负字符串?

这是代码....

    String display="";
    int 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

1 回答 1

1

这是你所追求的:

public class SO2{
public static void main(String[] args) {

 String display="";
 int 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]; //Sets array

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

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

    String toShow = ""; //String to build up
    for(int i = 0; i < array.length; i++){
        if(array[i] == 0){
            display = "ZERO";
        } else if(array[i] < 0){
            display = "NEGATIVE";
        } else if(array[i] > 0){
            display = "POSITIVE";
        }

        toShow += "Array element " + i + " is " + array[i] + " and it is " + display + "\n"; //Build string
    }
    JOptionPane.showMessageDialog(null,"Your numbers...\n\n"+toShow);//show
}}

它显示了数组中的所有数字及其旁边的 POS/NEG/ZERO 值。我添加了一些评论来尝试解释一下

祝你好运!

于 2013-11-13T16:09:14.933 回答