如何在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:使用静态输入值
在这种方法中,我们声明一个具有静态输入值的变量,然后使用算法检查该数字是否为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数。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP