JAVA语言 判断一组数字是否有相同的数字?

Python08

JAVA语言 判断一组数字是否有相同的数字?,第1张

public class Test {

public static boolean judgment(String numStr) {

char[] ch = numStr.toCharArray()

for (int i = 0i <ch.lengthi++) {

char last = ch[i]

for (int j = i + 1j <ch.lengthj++) {

if (last == ch[j])

return true

else

continue

}

}

return false

}

public static void main(String[] args) {

Scanner sc = new Scanner(System.in)

System.out.println("请输入一个整形数字:")

int input = sc.nextInt()

if (judgment(String.valueOf(input)))

System.out.println("您输入的数字包含重复数字!")

else

System.out.println("您输入的数字没有重复数字!")

sc.close()

}

}

无非就是检查是否已经有数据的问题,用List做可能比较简单点

public class SigleList{

    List<Integer> nums

    

    public SingleList(){

        nums = new ArrayList<Integer>()

    }

    

    public void add(Integer num){

        if(!checkNumExist){

            nums.add(num)

        }

    }

    

    public boolean checkNumExist(Integer){

        return nums.contains(Integer)

    }

}

这个SingleList就简单实现了去重增加,可以用它来读数据

public class Test{

    public static void main(String[] args){

        SingleList nums = new SingleList()

    

        // 产生100个数据

        for(int i = 0 i++ i < 100){

            // 产生随机数

            Integer num = new Random().nextInt(100000)

            nums.add(num)

        }

        

        foreach(Integer num : nums){

            System.out.println(num)

        }

    }

}