国庆假期后课堂小测:输入一个整数并判断位数,位数和
源代码:
package 测试;
import java.util.Scanner;
public class test
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
String number;
System.out.println("输入一个数");
number=input.nextLine();
NumberJudge num=new NumberJudge();
num.qushu(number);
int sum=0;
sum=num.qiuhe(number);
System.out.println("各位数之和为:"+sum);
}
}
class NumberJudge
{
private int num[];
public NumberJudge()
{
num=new int[10];
}
public int calculate_places(String number)
{
int places;
places=number.length();
return places;
}
public void qushu(String number)
{
int places;
places=number.length();
System.out.println("位数为:"+calculate_places(number)+"位");
System.out.println("从高位到低位依次输出每位的值:");
for(int i=0;i<places;i++)
{
num[i]=number.charAt(i)-48;
System.out.print(num[i]+" ");
}
System.out.println();
}
public int qiuhe(String number)
{
int places;
places=number.length();
int sum=0;
for(int i=0;i<places;i++ )
{
sum=sum+num[i];
}
return sum;
}
}
运行截图: