为了账号安全,请及时绑定邮箱和手机立即绑定

检查号码是否是唯一号码

检查号码是否是唯一号码

米琪卡哇伊 2023-07-13 14:16:10
我编写了一个程序来检查一个数字是否是唯一的数字。[唯一数字是没有重复数字和前导零的数字。]我编写了以下代码:    Scanner sc=new Scanner(System.in)    System.out.println("Enter the number to be checked: ");    String num=sc.nextLine();    if(num.charAt(0)!='0')    {           Outer:        for(int i=0;i<num.length();i++)        {            for(int j=0;j<num.length();j++)            {                if(num.charAt(i)==num.charAt(j))                {                    System.out.println("No, "+num+" is not a Unique number.");                    break Outer;                }            }            if(i==num.length()-1)            {                System.out.println("Yes, "+num+" is a Unique number.");            }        }    }    else        System.out.println("No, "+num+" is not a Unique number as it has leading zeros.");问题是,任何数字都显示为不唯一,甚至 12345。我想知道我哪里出了问题。
查看完整描述

5 回答

?
素胚勾勒不出你

TA贡献1827条经验 获得超9个赞

假设输入数字的长度为 10,并且“i”在 for 循环中已达到值 5。

现在“j”的值为 0 到 9。

因此,当“j”等于 5 时,当您将第 5 个位置的数字与其自身进行比较时,if 条件变为 true(这始终为 true)。

  • 如果添加 i != j 条件,它将解决问题:-

if(num.charAt(i)==num.charAt(j) and i != j)

  • 或者,您可以修改 j 的循环,使其从 i + 1 开始,这样就不会出现重叠。

for(int j=i+1;j<num.length();j++)

第二个选项要好得多,因为它将减少从 (n*n) 到 (n * (n - 1))/2) 的比较次数,其中 n 是输入数字中的位数。


查看完整回答
反对 回复 2023-07-13
?
慕容森

TA贡献1853条经验 获得超18个赞

一种可能的解决方案是使用Stream将您的字符转换String为a Set,然后如果集合的大小与字符串的长度相同,那么它是唯一的:

Scanner sc = new Scanner(System.in);

System.out.println("Enter the number to be checked: ");

String num = sc.nextLine();


boolean unique = Stream.of(num.split(""))

    .map(s -> new String(s))

    .collect(Collectors.toSet()).size() == num.length();

// With "1234" -> print true

// With "12342" -> print false

System.out.println(unique);


查看完整回答
反对 回复 2023-07-13
?
aluckdog

TA贡献1847条经验 获得超7个赞

当 时,您的代码将始终找到“重复”字符i == j。


您应该更改循环的索引,以免将字符与其自身进行比较:


for(int i=0;i<num.length();i++) {

    for(int j=i+1;j<num.length();j++) {

        if(num.charAt(i)==num.charAt(j))

            ...

此外,您应该只输出“...是一个唯一的数字”。完成外循环后的消息。


查看完整回答
反对 回复 2023-07-13
?
墨色风雨

TA贡献1853条经验 获得超6个赞

您可以使用以下简短而方便的方法:


    String a = "123452";


    String[] split = a.split("");

    List<String> list = Arrays.asList(a.split(""));

    Set<String> set = new HashSet<>(list);


    System.out.println("Unique: " + (list.size() == set.size()));


查看完整回答
反对 回复 2023-07-13
?
慕少森

TA贡献2019条经验 获得超9个赞

import java.util.*;


public class spnum

{

    public static void main(String[] args)

    {

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter a number: ");

        String num = sc.next();

        int ctr = 0;

        boolean isNumUnique = true;

        for(int i = 0; i < num.length(); i++)

        {

            for(int j = 0; j < num.length(); j++)

            {

                if(num.charAt(i) == num.charAt(j))

                {

                    ctr++;

                }

            }

            if(ctr > 1)

            {

                isNumUnique = false;

            }

            ctr = 0;

        }


        if(isNumUnique == true)

        {

            System.out.println("Number is a unique number");

        }

        else

        {

            System.out.println("Number is not a unique number");

        }

    }

}

这段代码会给出正确的答案


查看完整回答
反对 回复 2023-07-13
  • 5 回答
  • 0 关注
  • 110 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信