首頁  >  文章  >  Java  >  Java程式提取給定整數的數字

Java程式提取給定整數的數字

WBOY
WBOY轉載
2023-08-29 19:33:031211瀏覽

Java程式提取給定整數的數字

在Java程式設計中,有一些情況我們需要從一個整數中提取單一數字以進行進一步的操作或分析。本教學將指導您如何使用Java從給定的整數中提取數字。

Syntax

while (num > 0) {
int digit = num % 10;
   System.out.println(digit);
   num = num / 10;
}

The above is the syntax to extract digits from an integer in Java. We keep extracting the last digit by taking the remainder of the number with 10. We divide the number by 10 until it becomes 0.

Extract Digits Using Division and Modulus

從整數中提取數字的常見方法是使用除法和取模運算。其想法是重複將數字除以10,提取餘數(表示最右邊的數字),然後透過移除提取的數字來更新數字。

Example

的中文翻譯為:

範例

Let's consider the integer num = 1234. By applying the above algorithm, we can extract each digit and perform desired operations on them

Explanation

的中文翻譯為:

解釋

  • We initialize an integer variable num with the value 1234.

  • Using a while loop, we iterate until the num is greater than 0.

  • #在每次迭代中,我們使用模運算子(%)和10來提取num的當前值的最後一位數字。這個運算給了當num被10除時的餘數,表示最後一位數字。

  • 提取的數字儲存在變數 digit 中。

  • 我們使用System.out.println(digit)來顯示數字的值,它會在控制台上列印出該數字。

  • 提取最後一位數字後,我們透過使用整數除法運算子(/)將num的值除以10來更新num的值。這將從num中刪除最後一位數字,並為下一次迭代做準備。

  • The loop continues until num becomes 0, indicating that all digits have been extracted and displayed.

public class Main {
   public static void main(String args[]) {
      int num = 1234;
      while (num > 0) {
         int digit = num % 10;
         System.out.println(digit);
         num = num / 10;
      }
   }
}

Output

#
4
3
2
1

使用內建方法提取數字

In this scenario, we will extract the individual digits from an integer and display them on the console.

Example 1

的中文翻譯為:

範例 1

Let's consider an integer, num = 34512. We will extract and display the digits individually.

Explanation

的中文翻譯為:

解釋

  • We first convert the integer num to a string representation using Integer.toString(num).

  • Next, we iterate through each character in the string using a for loop.

  • #For each character, we check if it is a digit using Character.isDigit().

  • If it is a digit, we convert it back to an integer using Character.getNumericValue() and store it in the digit variable.

  • 最後,我們使用System.out.println()顯示每個數字。

public class Main {
   public static void main(String[] args) {
	int num = 34512;
	String numString = Integer.toString(num);

	for (int i = 0; i < numString.length(); i++) {
	   if (Character.isDigit(numString.charAt(i))) {
	      int digit = Character.getNumericValue(numString.charAt(i));
            System.out.println("Digit: " + digit);
         }
      }
   }
}

Output

#
Digit: 3
Digit: 4
Digit: 5
Digit: 1
Digit: 2

Example 2

讓我們考慮另一個整數,num = 987654。我們將逐一提取並顯示數字。

Explanation

的中文翻譯為:

解釋

  • We convert the integer num to a string representation using String.valueOf(num).

  • 而不是使用基於索引的循環,我們利用增強的for循環來遍歷字串中的每個字元。

  • For each character, we check if it is a digit using Character.isDigit().

  • If it is a digit, we convert it back to an integer using Character.getNumericValue() and store it in the digit variable.

  • 最後,我們使用System.out.println()顯示每個數字。

public class Main {
   public static void main(String[] args) {
	int num = 987654;
	String numString = String.valueOf(num);
	for (char digitChar : numString.toCharArray()) {
	   if (Character.isDigit(digitChar)) {
            int digit = Character.getNumericValue(digitChar);
            System.out.println("Digit: " + digit);
	      }
	   }
   }
}

Output

#
Digit: 9
Digit: 8
Digit: 7
Digit: 6
Digit: 5
Digit: 4

結論

在本教程中,我們探討了在Java中從給定整數中提取數字的過程。我們涵蓋了兩種不同的情況:使用除法和模運算提取數字以及提取和顯示單個數字。

透過應用除法和取模的方法,我們學會如何透過重複將數字除以10並使用餘數作為數字來提取數字。這種技術使我們能夠有效率地提取和操作整數的數字。

我們也探索了將數字單獨提取和顯示的情況。透過將整數轉換為字串表示,並透過迭代每個字符,我們可以檢查數字並相應地顯示它們。這種方法在進一步處理或分析提取的數字時提供了靈活性。

以上是Java程式提取給定整數的數字的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除