1.问题重述
题目:取一个整数a从右端开始的4~7位。
2.解析
使用求余符号%依次取出数字各个位置的位数,然后再用for循环遍历输出特定的几位数的数字。
3.解决问题
代码如下:
//题目:取一个整数a从右端开始的4~7位。
public class demo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入数字:");
long number = scanner.nextLong();
int[] array = new int[7];
//依次存储各个位子上的数字
for(int i = 0;i < 7;i++) {
array[i] = (int) (number % 10);
number /= 10;
}
System.out.print("从右端开始的4~7位是:");
//遍历输出第4~7位
for(int i = 3;i < 7;i++) {
System.out.print(array[i] + " ");
}
}
}