2014-10-11 180 views
0

我很抱歉,如果这已经被问过,但我无法找到一个决定性的答案经过一些广泛的搜索,所以我想我会问这里。我是Java的初学者(通常是编码),他的任务是编写一个程序,它接受用户输入的3位数字,并添加这三位数字。如何使用Java将整数分割为数组?

注意:我不能使用循环执行此任务,并且三位数字必须全部输入一次。

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); 
+2

为什么你想创建一个数组?为什么不把每个数字从输入字符串中拉出来并添加呢?这听起来像一个[XY问题](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)给我。 – azurefrog 2014-10-11 21:49:29

回答

0

你可以使用整数运算拿出三个数字seperately

int first = threedigitinput/100; 
int second = (threedigitinput % 100)/10; 
int third = threedigitinput % 10; 
+0

是一种矫枉过正的情况,因为他可以简单地提取数字并添加它们。 – 2014-10-11 21:57:31

+0

我知道,但这是我想到的第一件事,就是他所说的问题已经完成了 – JRowan 2014-10-11 21:58:32

0

使用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) }; 
    } 
} 
+1

没有循环。简单的字符串解析和添加应该做到这一点。 – 2014-10-11 21:59:49

1

有多种方法,其中之一将...

String ss[] = "123".split(""); 
    int i = 
      Integer.parseInt(ss[0]) + 
      Integer.parseInt(ss[1]) + 
      Integer.parseInt(ss[2]); 
    System.out.println(i); 

另一个是...

String s = "123"; 
    int i = 
      Character.getNumericValue(s.charAt(0)) + 
      Character.getNumericValue(s.charAt(1)) + 
      Character.getNumericValue(s.charAt(2)); 
    System.out.println(i); 

还有一个是...

String s = "123"; 
    int i = 
      s.charAt(0) + 
      s.charAt(1) + 
      s.charAt(2) - 
      (3 * 48); 
    System.out.println(i); 

但硬编码的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())); 
} 

输出对于每个例如:6

0

如果我理解你的问题,你可以使用Character.digit(char,int)喜欢的东西让每个字符的值 -

int value = Character.digit(myInput.charAt(0), 10) 
     + Character.digit(myInput.charAt(1), 10) 
     + Character.digit(myInput.charAt(2), 10); 
相关问题