编写一个程序,使它能够读入10个数并且显示其中不同的数。提示:读入一个数,如果它是一个新数则把他存储在数组中,如果数组中已有该数,则把它丢弃。

解决方案 »

  1.   

    使用Set<Integer>接口下的类,这是一个不重复集合,会自动抛弃重复数据咩
      

  2.   


    class Test {    public static void main(String[] args) {
            Integer[] in = new Integer[10];
            Random r = new Random();
            //做数据
            for (int i = 0; i < 10; i++) {
                in[i] = r.nextInt(10) + 1;
            }
            Integer[] out = check(in);
        }
        
        static Integer[] check(Integer[] i) {
            Set<Integer> set = new HashSet<Integer>();
            for (int j : i) {
                set.add(j);
            }
            return set.toArray(new Integer[set.size()]);
        }
    }
      

  3.   

    直接向Set集合里加入数据,碰上重复的Set集合会自动处理的,你只管add就行咩...
      

  4.   

        Set<Integer> set = new HashSet<Integer>();没看懂!
      

  5.   

    import javax.swing.JOptionPane;
    public class Example5_5 
    {
        public static void main(String[] s)
        {
           int count=10;
           int[] number=new int[count];
           int i=0,j=0;
           boolean equal=false;
          String output=" ";
           for(i=0;i<count;i++)
           {
          String numberstring=JOptionPane.showInputDialog(null,"Example5_5","Please input the different number:",JOptionPane.QUESTION_MESSAGE);
          number[i]=Integer.parseInt(numberstring);
            }
           for(i=0;i<10;i++)
           {
         equal=false;
            for(j=i+1;j<10;j++)
            { 
             if(number[j]==number[i])
              
                equal=true;
                break;
             }
          
            if(equal==false)
             output+=number[i]+"  ";
           }
          JOptionPane.showMessageDialog(null,output,"the different number:",JOptionPane.INFORMATION_MESSAGE);
         }
    }