題目:取一個整數 a 從右端開始的 4~7 位。
public class WangDao {
public static void main(String[] args) {
System.out.print("Input a long integer number: ");
Scanner scan = new Scanner(System.in);
long l = scan.nextLong(); // 輸入一個長整型。
String str = Long.toString(l); // 將長整型數字轉換成字符串。
char[] ch = str.toCharArray(); // 將字符串轉換成字符數組。
int n = ch.length; // 用n保存字符數組的個數。
if (n < 7) {
System.out.println("The number you entered is less than 7 digits.");
} else { //輸出從右端開始數的4-7位。
System.out.println("4-7 bits from the right are " + ch[n-4] + ch[n-5] + ch[n-6] + ch[n-7]);
}
}
}