猿问

检查号码是否是唯一号码

我编写了一个程序来检查一个数字是否是唯一的数字。[唯一数字是没有重复数字和前导零的数字。]


我编写了以下代码:


    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。我想知道我哪里出了问题。


米琪卡哇伊
浏览 149回答 5
5回答

素胚勾勒不出你

假设输入数字的长度为 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 是输入数字中的位数。

慕容森

一种可能的解决方案是使用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 falseSystem.out.println(unique);

aluckdog

当 时,您的代码将始终找到“重复”字符i == j。您应该更改循环的索引,以免将字符与其自身进行比较:for(int i=0;i<num.length();i++) {&nbsp; &nbsp; for(int j=i+1;j<num.length();j++) {&nbsp; &nbsp; &nbsp; &nbsp; if(num.charAt(i)==num.charAt(j))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...此外,您应该只输出“...是一个唯一的数字”。完成外循环后的消息。

墨色风雨

您可以使用以下简短而方便的方法:&nbsp; &nbsp; String a = "123452";&nbsp; &nbsp; String[] split = a.split("");&nbsp; &nbsp; List<String> list = Arrays.asList(a.split(""));&nbsp; &nbsp; Set<String> set = new HashSet<>(list);&nbsp; &nbsp; System.out.println("Unique: " + (list.size() == set.size()));

慕少森

import java.util.*;public class spnum{&nbsp; &nbsp; public static void main(String[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Scanner sc = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter a number: ");&nbsp; &nbsp; &nbsp; &nbsp; String num = sc.next();&nbsp; &nbsp; &nbsp; &nbsp; int ctr = 0;&nbsp; &nbsp; &nbsp; &nbsp; boolean isNumUnique = true;&nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0; i < num.length(); i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int j = 0; j < num.length(); j++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(num.charAt(i) == num.charAt(j))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ctr++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(ctr > 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isNumUnique = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ctr = 0;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(isNumUnique == true)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Number is a unique number");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Number is not a unique number");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}这段代码会给出正确的答案
随时随地看视频慕课网APP

相关分类

Java
我要回答