首页 > Java > java教程 > 正文

Java程序提取给定整数的数字

WBOY
发布: 2023-08-29 19:33:03
转载
1237 人浏览过

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
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!