用java编写一个程序,来验证用户输入的数是不是2的阶次方

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.stella;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
 * 获取用户输入的数
 * @author Administrator
 *
 */
public class GetUserInput {
    private static String str;
    /**
     * 获取用户输入
     */
    private static void getUserInput(){
        BufferedReader rb = new BufferedReader(new InputStreamReader(System.in));
        try {
            str = rb.readLine();
             
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("io异常!");
        }
    }
    /**
     * 将用户输入的字符串类型转换为Integer类型
     * @return
     */
    public static Integer getInt(){
        getUserInput();
        if(str == null || str.trim().equals("")){
            throw new RuntimeException("用户输入不能为空!");
        }
        try{
            return Integer.valueOf(str);
        }catch(NumberFormatException e){
            throw new NumberFormatException("您输入的类型有误!");
        }
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.stella;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
 
public class Test {
    public static void main(String[] args) {
         
        while(true){
            System.out.println("请输入一个整数");
            int num = GetUserInput.getInt();
            boolean a = judge(num);
            if(a){
                System.out.println("这个数是2的阶乘");
            }else{
                System.out.println("这个数不是2的阶乘");
            }
        }
    }
    /**
     * 获得用户输入的一个整数,然后计算它的平方和立方
     */
    public static void value(){
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try {
            String str = br.readLine();
            if(str != null && !str.equals("")){
                int v = Integer.valueOf(str);
                print(v);
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
         
         
    }
    /**
     * 求一个数的平方和立方
     * @param is
     */
    public static void print(int is){
        System.out.println(is + "的平方值和立方值:");
        System.out.println("平方值:" + is*is);
        System.out.println("立方值:" + is*is*is);
    }
    /**
     * 判断一个数是不是2的阶乘
     * 原理是:如果一个书是2的阶乘,则这个数减1和它自己进行“&”位运算结果为0
     * @param x
     * @return
     */
    public static boolean judge(Integer x){
        if((x & (x - 1)) == 0 && x != null){
            return true;
        }
        return false;
    }
}

  

用java编写一个程序,来验证用户输入的数是不是2的阶次方,布布扣,bubuko.com

用java编写一个程序,来验证用户输入的数是不是2的阶次方

上一篇:python进阶七_文件操作(三)


下一篇:java20140406