在数组中搜索int

时间:2014-06-19 18:31:13

标签: arrays search

我正在尝试在具有随机数的数组中搜索int值 这就是我到目前为止所做的:

String names[]={"Peter","John","Rudy"};
int number[]=new int[3];
for(int z=0;z<3;z++)
{
    number[z]=(int)(1+Math.random()*200);
}
int option=Integer.parseInt(JOptionPane.showInputDialog("choose and option:\n1.names\n2.sorting according to alphabet\n3.search number\n4.J"));
switch(option)
{
    case 1:
    {
        for(int l=0;l<names.length;l++)
        {
        jTextArea1.append(""+names[l]+"\t"+number[l]+"\n");
        }
    }
    break;
    case 2:
    {
        String temp="";
        for(int ii=0;ii<names.length;ii++)
        {
            for(int j=0;j<names.length;j++)
            {
                if(names[ii].compareToIgnoreCase(names[j])<0)
                {
                    temp=names[ii];
                    names[ii]=names[j];
                    names[j]=temp;
                }
            }
        }
        for(int x=0;x<names.length;x++)
        {
            jTextArea1.append(""+names[x]+"\n");
        }
    }
    break;
    case 3:
    {
        boolean found=false;
       int searchvalue=Integer.parseInt(JOptionPane.showInputDialog("number?"));
       for(int i=0;i<number.length;i++)
       {
           if(number[i]==searchvalue)

           {

               found=true;
           }
       }
       if(found==true)
       {
           jTextArea1.append("number is found"+"\n");
       }
       else
       {
           jTextArea1.append("number is not found"+"\n");
       }
    }
    break;
    case 4:
    {
        for(int q=0;q<names.length;q++)
        {
            if(names[q].startsWith("J"))
            {
                jTextArea1.append(names[q]);
            }
        }
    }

}

即使我输入正确的答案,它也会给我&#34;号码找不到&#34;我很惊讶于该怎么做。非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

尝试这样的事情。我不确定这是否正是你要找的......

        public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        boolean found = false;
        int searchvalue;
        int[] lottoNumber = new int[6];  
        lottoNumber[0] = (int) ((56 * Math.random()) + 1);  
        lottoNumber[1] = (int) ((56 * Math.random()) + 1);  
        lottoNumber[2] = (int) ((56 * Math.random()) + 1);  
        lottoNumber[3] = (int) ((56 * Math.random()) + 1);  
        lottoNumber[4] = (int) ((56 * Math.random()) + 1);  
        lottoNumber[5] = (5);
        System.out.print("number?");
        searchvalue = input.nextInt();
        for (int i = 0; i < lottoNumber.length; i++) {
            if (lottoNumber[i] == searchvalue) {

                found = true;
            }
        }
        if (found == true) {
            System.out.print("number is found" + "\n");
        } else {
            System.out.print("number is not found" + "\n");
        }
    }
}

顺便说一句,这里没有GUI导入。你究竟想用这个做什么?你能添加整个代码吗?

答案 1 :(得分:0)

上面的答案对我有用!所以答案是你的:

 int searchvalue=Integer.parseInt(JOptionPane.showInputDialog("number?"));

该行可能导致问题。

尝试添加:

System.out.println("The number entered was:"+searchvalue);

然后将其与您输入的数字进行比较。