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

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);
}
}

 
原文地址:https://www.cnblogs.com/xufeng123/p/6624364.html