首頁  >  文章  >  Java  >  如何在Java中檢查一個數字是否為斯特朗數?

如何在Java中檢查一個數字是否為斯特朗數?

PHPz
PHPz轉載
2023-08-26 18:21:031032瀏覽

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