1000以内の整数を入力して、各位の上で和(Java)を累加することを求めます

2395 ワード

/**
 * 
 */
package com.hengbao.one;

import java.util.Scanner;

/**
 * @author ttc
 * 
 */
public class Oracle {

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        try {
            while (true) {
                System.out.println("     0~1000   ");
                int i = s.nextInt();
                if (i < 0 || i > 1000) {
                    System.out.println("       ");
                } else {
                    int result = 0;//   
                    //           
                    while (i / 10 != 0)
                    {
                        result += i % 10;
                        i /= 10;
                    }
                    result += i % 10;
                    System.out.println("      " + result);
                }
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        } finally {
            System.out.println("      ");
            s.close();
        }
    }
}