取出数字中的每一位是编程中的常规操作;
12 / 10 = 【int(12 / 10)】 =【 int(1.2)】= 1;
但是 int 型会将小数点后的忽略(具体原因目前不太清楚);
这也是为什么可以用 “/ ” 配合 “ % ” 来实现精简的取数操作;
使用的情况
- 使用数字来控制灯的闪灭
A = 101010( 用0表示亮,1表示亮 );
A = 灭,暗,灭,暗,灭,暗,灭,暗;
取出每一位的数字,每隔一段时间赋给IO口;
可以实现流水灯操作
- 其他的还没想到
//输入五位数并取出每一位
package com.Tony;
import java.util.Scanner;
public class practice001 {
public static void main(String[]arge) {
Scanner sc = new Scanner(System.in);
System.out.println("Please input the number (five figures)");
int A = sc.nextInt();
int ge = A % 10;
int shi = A / 10 % 10;
int bai = A / 100 % 10;
int qian = A / 1000 % 10;
int wan = A / 10000;
System.out.println();
System.out.println("the number in the bits is " + ge);
System.out.println("the number in the 10-digit is " + shi);
System.out.println("the number in the 100-digit is " + bai);
System.out.println("the number in the 1000-digit is " + qian);
System.out.println("the number in the 10000-digit is " + wan);
}
}
输出结果