如何在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