Java 如何检查一个数字是否为Strontio数字

您所在的位置:网站首页 乘以2222 Java 如何检查一个数字是否为Strontio数字

Java 如何检查一个数字是否为Strontio数字

2024-07-13 15:09| 来源: 网络整理| 查看: 265

Java 如何检查一个数字是否为Strontio数字

如果我们把一个四位数与2相乘,那么在结果数字中,十位和百位的数字是相同的,这个数被称为Strontio数字。

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

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

在本文中,我们将通过Java编程语言来看如何检查一个数字是否为strontio数字。

示例

示例-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:通过使用静态输入值

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

示例 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.

在本文中,我们探讨了如何通过使用不同的方法来检查一个数字是否是斯特朗蒂奥数。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3