2008年8月29日 星期五

輸入一個數,算出所有數的總和

把輸入的數字一直除以10,剩下10以下的餘數再相加即可

code
package javahomework;

import javax.swing.JOptionPane;

public class HW04 {
    public static void main(String args[])
    {
       int input;
       input = Integer.parseInt(JOptionPane.showInputDialog(null,"Input Number"));
       JOptionPane.showMessageDialog(null,"The sum of "+input+" is "+ADD(input));
    }
    static int ADD(int n)
    {
        if (n<10)
            return n;
        else
            return (ADD(n/10)+ADD(n%10));
    }

}

沒有留言: