写一个方法,求给定的四位整数的各个位数之和

 

 

public class Homework07 {
void test2 (int a){
int t,x=0,i;
for(i=1;i<=4;i++){
t=a%10;
a=a/10;
x=x+t;

}
System.out.println("四位数的各位数和为"+x);
}
}

 

import java.util.Scanner;

public class Main07 {
public static void main(String[] args){
Homework07 y = new Homework07();
Scanner s = new Scanner(System.in);
System.out.println("a=");
int a=s.nextInt();
y.test2(a);
}
}

 
posted @ 2017-03-26 22:17  我为编程上架构  阅读(923)  评论(0编辑  收藏  举报