如何使用 Java 将整数拆分为数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26319579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to split an Integer into an Array using Java?
提问by Aarden Hartle
I apologise if this has already been asked before, but I was unable to find a conclusive answer after some extensive searching, so I thought I would ask here. I am a beginner to Java (to coding, in general) and was tasked with writing a program that takes a user-inputted 3 digit number, and adds those three digits.
如果之前已经问过这个问题,我深表歉意,但经过大量搜索后我无法找到结论性的答案,所以我想我会在这里问。我是 Java 的初学者(一般来说是编码),我的任务是编写一个程序,该程序接受用户输入的 3 位数字,并将这三位数字相加。
Note: I cannot use loops for this task, and the three digits must all be inputted at once.
注意:我不能在这个任务中使用循环,必须一次输入三个数字。
String myInput;
myInput =
JOptionPane.showInputDialog(null,"Hello, and welcome to the ThreeDigit program. "
+ "\nPlease input a three digit number below. \nThreeDigit will add those three numbers and display their sum.");
int threedigitinput;
threedigitinput = Integer.parseInt(myInput);
回答by ChiefTwoPencils
There are a number of ways, one of which would be...
有很多种方法,其中一种是...
String ss[] = "123".split("");
int i =
Integer.parseInt(ss[0]) +
Integer.parseInt(ss[1]) +
Integer.parseInt(ss[2]);
System.out.println(i);
another would be...
另一个将是...
String s = "123";
int i =
Character.getNumericValue(s.charAt(0)) +
Character.getNumericValue(s.charAt(1)) +
Character.getNumericValue(s.charAt(2));
System.out.println(i);
and still another would be...
还有一个是……
String s = "123";
int i =
s.charAt(0) +
s.charAt(1) +
s.charAt(2) -
(3 * 48);
System.out.println(i);
BUT hard coding for 3 numbers isn't very useful beyond this simple case. So how about recursion??
但是,除了这个简单的案例之外,3 个数字的硬编码并不是很有用。那么递归呢??
public static int addDigis(String s) {
if(s.length() == 1)
return s.charAt(0) - 48;
return s.charAt(0) - 48 + addDigis(s.substring(1, s.length()));
}
Output for each example: 6
每个示例的输出:6
回答by JRowan
you can use integer math to come up with the three numbers seperately
您可以使用整数数学来分别计算出这三个数字
int first = threedigitinput / 100;
int second = (threedigitinput % 100) / 10;
int third = threedigitinput % 10;
回答by Mr. Polywhirl
Classic example of using divmod
:
使用的经典示例divmod
:
public class SumIntegerDigits {
public static void main(String[] args) {
System.out.println(sumOfDigitsSimple(248)); // 14
System.out.println(sumOfDigitsIterative(248)); // 14
System.out.println(sumOfDigitsRecursive(248)); // 14
}
// Simple, non-loop solution
public static final int sumOfDigitsSimple(int x) {
int y = x % 1000; // Make sure that the value has no more than 3 digits.
return divmod(y,100)[0]+divmod(divmod(y,100)[1],10)[0]+divmod(y,10)[1];
}
// Iterative Solution
public static final int sumOfDigitsIterative(int x) {
int sum = 0;
while (x > 0) {
int[] y = divmod(x, 10);
sum += y[1];
x = y[0];
}
return sum;
}
// Tail-recursive Solution
public static final int sumOfDigitsRecursive(int x) {
if (x <= 0) {
return 0;
}
int[] y = divmod(x, 10);
return sumOfDigitsRecursive(y[0]) + y[1];
}
public static final int[] divmod(final int x, int m) {
return new int[] { (x / m), (x % m) };
}
}
回答by Elliott Frisch
If I understand your question, you could use Character.digit(char,int)
to get the value for each character with something like -
如果我理解你的问题,你可以用Character.digit(char,int)
类似的东西来获取每个角色的价值 -
int value = Character.digit(myInput.charAt(0), 10)
+ Character.digit(myInput.charAt(1), 10)
+ Character.digit(myInput.charAt(2), 10);