取一个整数a从右端开始的4~7位详解编程语言

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

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

 

 

 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 }

 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/11327.html

(0)
上一篇 2021年7月19日 11:01
下一篇 2021年7月19日 11:01

相关推荐

发表回复

登录后才能评论