2014-10-11 239 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);