首页 > Java > java教程 > 正文

如何在Java中检查一个数是否是斯特朗数?

PHPz
发布: 2023-08-26 18:21:03
转载
1036 人浏览过

如何在Java中检查一个数是否是斯特朗数?

一个数字被称为 Strontio 数,如果我们将 2 与一个四位数相乘,则所得数字的十位和百位数字相同。

简单来说,Strontio 号码基本上是四位数字。当我们乘以 2 时,得到的结果是一个十位和百位完全相同的数字。

Strontio 号码的一些示例包括 - 1111、2222、3333、4444、5555、6666、7777、8888、9999、1001...等。

在本文中,我们将了解如何使用 Java 编程语言检查一个数字是否为强数字。

向您展示一些实例

实例1

输入号码为1111。

让我们用Strontio数的逻辑来检查一下。

将给定数字乘以 2= 1111 * 2 = 2222。

正如我们在这里注意到的,百位和十位的结果数字是相同的。

因此,1111 是一个 Strontio 号码。

实例2

输入号码为7777。

让我们用Strontio数的逻辑来检查一下。

将给定数字乘以 2= 7777 * 2 = 15554。

正如我们在这里注意到的,百位和十位的结果数字是相同的。

因此,7777 是一个 Strontio 号码。

实例3

输入号码为1234。

让我们用Strontio数的逻辑来检查一下。

将给定数字乘以 2= 1234 * 2 = 2468。

正如我们在这里注意到的,百位和十位的结果数字并不相同。

因此,1234 不是 Strontio 号码。

算法

第 1 步 - 通过静态输入或用户输入获取四位输入号码。

第 2 步 - 将输入数字乘以 2,并从结果数字中删除除百位和十位之外的所有数字。

第 3 步 - 现在我们有一个两位数的数字,因此我们检查该数字的两位数是否相同。

步骤 4 - 如果两个数字相同,则输入的号码称为 Strontio 号码,否则不是。

多种方法

我们通过不同的方式提供了解决方案。

  • 通过使用静态输入值

  • 通过使用用户输入值

  • 通过用户定义的方法

让我们一一看看该程序及其输出。

方法 1:使用静态输入值

在这种方法中,我们声明一个具有静态输入值的变量,然后通过使用算法我们可以检查该数字是否是 Strontio 数字。

示例

import java.util.*;
public class Main {
   public static void main(String args[]) {
      
      //declare a variable and store a number to it by static input method
      int inputNumber=9999;
      
      //declare a variable to store the input value temporarily
      int temp=inputNumber;
      
      //remove all the digits excluding hundreds and tens place digit
      inputNumber=(inputNumber*2%1000)/10;
      
      //Now compare hundreds place value with tens place value
      if(inputNumber%10==inputNumber/10)
         
         //if true
         System.out.println(temp+ " is a strontio number.");
      else
         
         //if false
         System.out.println(temp+ " is not a strontio number.");
   }
}
登录后复制

输出

9999 is a strontio number.
登录后复制

方法 2:使用用户输入值

在这种方法中,我们将一个四位数字作为用户输入,然后通过使用算法我们可以检查该数字是否是 Strontio 数字。

示例

import java.util.*;
public class Main {
   public static void main(String args[]) {
      
      //create object of Scanner Class
      Scanner sc=new Scanner(System.in);
      
      //Ask the user to enter a four digit number
      System.out.print("Enter a four digit number: ");
      
      //Declare a variable and store the collected number
       int inputNumber=sc.nextInt();
      
      //declare a variable to store the input value temporarily
      int temp=inputNumber;
      
      //remove all the digits excluding hundreds and tens place digit
      inputNumber=(inputNumber*2%1000)/10;
      
      //Now compare hundreds place value with tens place value
      if(inputNumber%10==inputNumber/10)
         
         //if true
         System.out.println(temp+ " is a strontio number.");
      else
        
        //if false
         System.out.println(temp+ " is not a strontio number.");
   } 
}
登录后复制

输出

Enter a four digit number: 2222
2222 is a strontio number.
登录后复制

方法 3:使用用户定义的方法

在这种方法中,采用一个四位数字作为静态输入,并将该数字作为参数传递到用户定义的方法中,然后在该方法内部使用算法我们可以检查该数字是否是 Strontio 数字。

示例

import java.util.*;
public class Main {
   
   //main method
   public static void main(String args[]) {
      
      //declare a variable and store a number to it by static input method
      int inputNumber=3333;
      
      //call the user-defined method to check the strontio number
      if(checkStrontio(inputNumber))
         
         //print if condition true
         System.out.println(inputNumber+ " is a strontio number.");
      else
         
         //print if condition false
         System.out.println(inputNumber+ " is not a strontio number.");
   }

   //user-defined method to check the strontio number
   static boolean checkStrontio(int n) {
      
      //declare a variable to store the input value temporarily
      int temp=n;
      
      //remove all the digits excluding hundreds and tens place digit
      n=(n*2%1000)/10;
      
      //Now compare hundreds place value with tens place value
      if(n%10==n/10)
         
         //if true
         return true;
      else
         
         //if false
         return false;
   }
}
登录后复制

输出

3333 is a strontio number.
登录后复制

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

以上是如何在Java中检查一个数是否是斯特朗数?的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!