展开全部
主要的思想是先将这个整数对10求余,636f707962616964757a686964616f31333337396333得到个位数,再将整数除以10得到个位数以外的其余数(除以10之后因为数为int类型所以原先的个位数作为小数被舍去),然后重复操作得到其余位数
示例代码如下(因为要求个十百千位,所以没有写求更高位数的代码):
运行结果如下:
以下是文字版的代码:
import java.util.Scanner;
public class Test{
static int[] a=new int[4];//a[0]~a[3]:个位数~千位数
public static void main(String[] args){
Scanner in=new Scanner(System.in);
try{
System.out.print("输入一个整数");
int x=in.nextInt();
getValue(x);//求出个位数~千位数的方法
in.close();
}
catch(Exception e){
e.printStackTrace();
}
}
public static void getValue(int x){
int x1=x;
//先得到x的位数
int i=1,k,count=0;//count为x的位数
for(i=0;i<4;i++)//计算到千位即可
if(x1/Math.pow(10, i)!=0)
count++;
for(k=0;k
a[k]=x1%10;
x1=x1/10;
}
System.out.println(x+"的个位数:"+a[0]);
System.out.println(x+"的十位数:"+a[1]);
System.out.println(x+"的百位数:"+a[2]);
System.out.println(x+"的千位数:"+a[3]);
}
}