Java 如何检查一个数字是否是唯一数字

Java 如何检查一个数字是否是唯一数字

唯一数字可以定义为没有重复数字的数字。简单来说,这意味着该数字的所有数字都是唯一的,没有重复的数字。

在本文中,我们将看到如何使用Java编程语言检查一个数字是否是唯一数字。

为了向您展示一些示例

示例1

输入数字是145

让我们通过使用唯一数字的逻辑来检查它-

The digits are 1, 4, 5, all are unique digits.

因此,145是一个独特的数字。

示例2

输入的数字是37

通过使用独特数字的逻辑来检查它 –

The digits are 3, 7, all are unique digits.

因此,37是一个独特的数字。

示例3

输入数字为377

让我们通过使用独特数字的逻辑来检查它-

The digits are 3, 7, 7 all are not unique digits.

因此,377不是一个唯一的数字。

一些其他唯一数字的示例包括132、1、45、98、701、5、12、1234等等。

步骤

步骤1

  • 步骤1 - 通过初始化或用户输入获取一个整数。

  • 步骤2 - 进入一个外部while循环并获取最右边的数字。

  • 步骤3 - 进入另一个内部while循环,将步骤1的最右边的数字与左边的数字进行比较。 如果在任何时刻数字匹配,给定的数字有重复数字。 因此,打印给定的数字不是唯一的,然后重复步骤2和步骤3,直到步骤2涵盖数字的每个数字。

  • 步骤4 - 最后,如果找不到匹配项,则打印给定的数字是唯一的。

步骤2

  • 步骤1 - 通过初始化或用户输入获取一个整数。

  • 步骤2 - 接受一个外部for循环,逐个迭代每个数字。

  • 步骤3 - 接受一个内部for循环,并将外部for循环的最左边的数字与内部循环的所有最右边的数字进行比较。 如果在任何时刻数字匹配,则给定的数字有重复数字。 因此,打印给定的数字不是唯一的,然后重复步骤2和步骤3,直到步骤2涵盖数字的每个数字。

  • 步骤4 - 最后,如果找不到匹配项,则打印给定的数字是唯一的。

多种方法

我们以不同的方法提供了解决方案。

  • 通过逐个比较每个数字

  • 通过使用用户字符串

让我们逐一查看Java程序及其输出。

方法1:通过逐个比较每个数字

在这种方法中,程序将初始化一个整数值,然后使用 Algorithm-1 来检查一个数字是否是唯一的。

示例

public class Main{
   //main method
   public static void main(String[] args){

      //Declared an integer variable and initialized a number as value
      int originalNumber = 123;

      //printing the given number to be checked
      System.out.println("Given number: "+originalNumber);

      //keep a copy of original number
      int copyOfOriginalNumber = originalNumber;

      //initializing count as 1
      int count =0;

      //compare each digit with others
      //we will start comparing from rightmost digit with leftmost digits
      while (originalNumber > 0){

         //get the rightmost digit
         int digit = originalNumber % 10;

         //remove the rightmost digit and get the updated number
         originalNumber = originalNumber / 10;

         //assign the updated original number(rightmost digit removed) to a temp variable
         int temp = originalNumber;

         //check if the rightmost digit matches with leftmost digits
         while (temp > 0){

            //if rightmost digit matches with any leftmost digit then
            //increment count value
            //and break
            if (temp % 10 == digit){
               count = 1;
               break;
            }

            //remove rightmost digit from temp
            //and get updated temp value
            temp = temp / 10;
         }
      }
      //if count value is 1 then print given number is unique number
      if (count == 0){   
         System.out.println(copyOfOriginalNumber+" is a unique number");
      }

         //else print given number is not a unique number
      else {
         System.out.println(copyOfOriginalNumber+" is not a unique number");
      }
   }
}

输出

Given number: 123
123 is a unique number

方法2:使用字符串

在这个方法中,程序中将初始化一个整数值,然后通过使用 算法二 我们可以检查一个数字是否是彼得森数。

示例

public class Main{
   //main method
   public static void main(String[] args){

      //Declared an integer variable and initialized a number as value
      int originalNumber = 7890;

      //printing the given number to be checked
      System.out.println("Given number: "+originalNumber);

      //initialize count as 0
      int count = 0;

      //convert integer to String by using toString() method
      String st = Integer.toString(originalNumber);

      //Find length of String by using length() method
      int length=st.length();

      //check if the leftmost digit matches with rightmost digits
      for(int i=0;i<length-1;i++){
         for(int j=i+1;j<length;j++){

            //if matches then increment count and break
            if(st.charAt(i)==st.charAt(j)){
               count++;
               break;
            }
         }
      }
      //if count value is 1 then print given number is unique number
      if (count == 0){
         System.out.println(originalNumber+" is a unique number");
      }

         //else print given number is not a unique number
      else {
         System.out.println(originalNumber+" is not a unique number");
      }
   }
}

输出

Given number: 7890
7890 is a unique number

在本文中,我们探讨了如何使用不同的方法来检查一个数字是否是Java中的唯一数字。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程