【王道JAVA】【程序 32 左移右移】

題目:取一個整數 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]);
		}
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章