0
点赞
收藏
分享

微信扫一扫

取一个整数a从右端开始的4~7位

洛茄 2022-09-01 阅读 85


题目:取一个整数a从右端开始的4~7位。

程序分析:可以这样考虑:
先使a右移4位。
设置一个低4位全为1,其余全为0的数。可用~(~0 < <4)
将上面二者进行&运算。

 

 


1 package com.li.FiftyAlgorthm;
2
3 import java.util.Scanner;
4
5 public class FS {
6 public static void main(String[] args) {
7 Scanner s = new Scanner(System.in);
8 boolean is = true;
9 System.out.print("请输入一个7位以上的正整数:");
10 long a = s.nextLong();
11 String ss = Long.toString(a);
12 char[] ch = ss.toCharArray();
13 int j = ch.length;
14 if (j < 7) {
15 System.out.println("输入错误!");
16 } else {
17 System.out.println("截取从右端开始的4~7位是:" + ch[j - 7] + ch[j - 6]
18 + ch[j - 5] + ch[j - 4]);
19 }
20 }
21


 

举报

相关推荐

0 条评论