Java computer training questions
Beginners to learn Java, simple computer training questions
Question
: Write a program to set the initial value of variable n to 1678, and then use the division operation, area and operation to extract and print each digit of the variable. The output result is: n=1678 Each digit of n is
1
, 6, 7, 8
procedures are as shown in the figure
import java.io.*;//导入所需要的公用类
public class cla {
/**
* @param args
*/
public static void main(String[] args) throws IOException
{
// TODO Auto-generated method stub
//下面几行语句的作用是从键盘输入一个整数并存到变量f中
InputStreamReader ir;
BufferedReader in;
ir=new InputStreamReader(System.in);
in=new BufferedReader(ir);
String s=in.readLine();
int f=Integer.parseInt(s);
int j=s.length();//计算整数的长度
int x=f,i=0;
int [] a = new int [j];//定义一个一维数组存储整数的值
while(x>0)
{
a[i]=x%10;
x=x/10;
i++;
}
System.out.println("n="+f);
for(i=j-1;i>=0;i--)
{
if(i==j-1)
System.out.print("n的每位数字是");
System.out.print(a[i]);
if(i!=0)
System.out.print(", ");
}
}
}
The output result is