【王道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]);
		}
	}
}

猜你喜欢

转载自blog.csdn.net/YelloJesse/article/details/89430781
今日推荐